我试图用ggplot2包中心我的酒吧.条形图未与相应值的中心对齐,这可能导致对非专业读者的一些误解.我的情节看起来像这样:
为了重现该图,请使用以下代码:
# 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))
Run Code Online (Sandbox Code Playgroud)
我如何将每个条形图集中到相应的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))
Run Code Online (Sandbox Code Playgroud)
Michael Kuhn 在 2015 年 12 月的回答现在正在返回警告:
origin已弃用。请boundary改用。
您现在可以指定边界(即左侧或右侧的位置)或箱的中心。原点已被弃用,以支持这些论点。
因此,对于ggplot22.1.0 及更高版本,建议的前进方式是使用以下任一boundary参数:
library(ggplot2) # CRAN version 2.2.1 used
ggplot(data=Temp, aes(Score, fill = ..count..)) +
geom_histogram(col = "grey", binwidth = 1, boundary = 0.5) +
scale_fill_gradient("Count", low = "green", high = "red") +
labs(title = "Title") +
labs(x = "X-Title", y = "Y-Title") +
xlim(c(3, 9))
Run Code Online (Sandbox Code Playgroud)
或者在center参数
...
geom_histogram(col = "grey", binwidth = 1, center = 0) +
...
Run Code Online (Sandbox Code Playgroud)
结果是一样的:
BTW:我已删除的OP代码一个小小的瑕疵,其使用Temp$Score的aes()呼叫ggplot(data=Temp, aes(Temp$Score)),而不是ggplot(data = Temp, aes(Score))。
由于下载链接将来可能会中断,以下是返回的数据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))
Run Code Online (Sandbox Code Playgroud)
只需删除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))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4074 次 |
| 最近记录: |