[英]ggplot2 bar chart won't display spaces between bars
我正在尝试在ggplot2中创建条形图,以显示大型数据集的直方图。
ggplot(data = score.data, aes(x = score)) + geom_bar(binwidth = .25, width=.9)
我的雇主的图形设计政策是使条形图之间有少量分隔。 但是,无论我将width参数设置为什么,都会生成条形图,并且条形之间没有任何间隔。 我应该使用其他参数来完成此操作吗?
编辑:在评论中jeremycg的建议之后,我包括了一些数据。
score <- c(6.25, 4.75, 6, 1.5, 6, 5, 6, 2.75, 6, 2.5, 5.25, 6, 3.75, 6,
6.25, 5, 6.75, 2, 2.75, 3.25, 5.625, 6.5, 4, 5, 3)
我将其转换为一个因子,除了我要显示大小为.25的bin-四分之一之间的值很少(如您在上面看到的,其中一个值为5.625)。 stat_bin
仅适用于ggplot认为是连续的数据。
假设您具有数字数据而不是整数,则可以在geom_bar
或geom_histogram
中将colour = "white"
设置colour = "white"
size = 1.5
(或另一个值)。 这将使条形的边缘变白且变粗(使用size参数),从而在它们之间创建一些可视空间:
# create some sample data
set.seed(2)
data <- data.frame(score=rnorm(1000,0,5), int.score=sample(1:10,1000,replace=TRUE))
# create the plot
ggplot(data, aes(x = score)) +
geom_bar(binwidth = .25, color="white", size=1.5) +
theme_bw()
这给出了:
当您有整数时,您也可以在注释中使用@jeremycg提及的技巧:
ggplot(data, aes(x = factor(int.score))) +
geom_bar(width=0.7) +
theme_bw()
这使:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.