gpt4 book ai didi

r - 使用 ggplot2 的直方图中心条

转载 作者:行者123 更新时间:2023-12-03 22:58:17 30 4
gpt4 key购买 nike

我试图用 ggplot2 包居中我的酒吧。条形未与相应值的中心对齐,这可能会导致非专业读者产生一些误解。我的情节是这样的:

enter image description here

为了重现情节,请使用以下代码:

# Load data
Temp <- read.csv("http://pastebin.com/raw.php?i=mpFpjqJt", header = TRUE, stringsAsFactors=FALSE, sep = ";")
# Load package
library(ggplot2)
# Plot histogram using ggplot2
ggplot(data=Temp, aes(Temp$Score)) +
geom_histogram(breaks=seq(0, 8, by =1), col="grey", aes(fill=..count..), binwidth = 1, origin = -0.5)
+ scale_fill_gradient("Count", low = "green", high = "red")
+ labs(title="Title")
+ labs(x="X-Title", y="Y-Title")
+ xlim(c(3,9))

如何将每个条形居中到相应的 x 值?

编辑 2017-05-29
由于下载链接以后可能会断,这里是 dput()返回的数据
Temp <- structure(list(ID = 1:30, Score = c(6L, 6L, 6L, 5L, 5L, 5L, 6L, 
5L, 5L, 5L, 4L, 7L, 4L, 6L, 6L, 6L, 6L, 6L, 5L, 5L, 7L, 5L, 6L,
5L, 5L, 5L, 4L, 6L, 6L, 5L)), .Names = c("ID", "Score"), class = "data.frame",
row.names = c(NA, -30L))

最佳答案

只需删除 breaks参数,这是多余的/与 binwidth 冲突和 origin论据:

# Load data
Temp <- read.csv("http://pastebin.com/raw.php?i=mpFpjqJt", header = TRUE, stringsAsFactors=FALSE, sep = ";")
# Load package
library(ggplot2)
# Plot histrogram using ggplo2
ggplot(data=Temp, aes(Temp$Score)) +
geom_histogram(col="grey", aes(fill=..count..), binwidth = 1, origin = -0.5) +
scale_fill_gradient("Count", low = "green", high = "red") +
labs(title="Title") +
labs(x="X-Title", y="Y-Title") +
xlim(c(3,9))

关于r - 使用 ggplot2 的直方图中心条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34180300/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com