我正在尝试使用下面的数据集创建条形图
df = structure(list(Affiliation = structure(c(1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L), .Label = c("BMI", "CCS",
"CS", "Epi", "Genom", "HSE",
"HSR", "HPR"), class = "factor"),
count = structure(c(4L, 21L, 14L, 20L, 11L, 13L, 19L, 15L,
5L, 22L, 17L, 24L, 9L, 12L, 18L, 16L, 1L, 10L, 7L, 23L, 2L,
3L, 8L, 6L), .Label = c("15", "26", "27", "32", "40", "41",
"42", "58", "62", "63", "70", "88", "89", "96", "99", "112",
"125", "160", "164", "172", "176", "178", "200", "628"), class = "factor"),
Year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("2014",
"2015", "2016"), class = "factor")), .Names = c("Affiliation",
"count", "Year"), row.names = c(NA, 24L), class = "data.frame")
这是我到目前为止所做的事情
ggplot(df, aes(x = Affiliation, y = count, fill = Year, group = Year)) +
geom_bar(position = position_dodge(width = 0.9), stat = "identity", alpha = 1,
size = 1, width = 0.05) +
geom_text(aes(label = count), position = position_dodge(width = 0.9),
vjust = -0.25) + scale_fill_brewer(palette = "Set1")
但是,我正在尝试基于此示例为此条形图创建可视化:
在本文中讨论过:Looking for better way to visualise distribution in R and ggplot2
答案 0 :(得分:7)
我还不完全确定你的问题是什么。创建棒棒糖图表的示例不使用geom_bar()
。这是你之后的事吗?
# Why did you have this as a factor?
df$count <- as.numeric(as.character(df$count))
gg <- ggplot(df, aes(Affiliation, count))
gg <- gg + geom_segment(aes(xend=Affiliation, yend=0))
gg <- gg + geom_point()
gg <- gg + geom_text(aes(label=count, y=count+25), vjust=0, size=3)
gg <- gg + scale_y_continuous(expand=c(0,0), limits=c(0, 800))
gg <- gg + facet_wrap(~Year)
gg <- gg + labs(x=NULL, y=NULL)
gg <- gg + theme_bw()
gg <- gg + theme(strip.background=element_blank())
gg <- gg + theme(strip.text=element_text(hjust=0))
gg <- gg + theme(panel.grid.major.x=element_blank())
gg <- gg + theme(panel.grid.minor.y=element_blank())
gg <- gg + theme(axis.ticks=element_blank())
gg <- gg + theme(axis.text.x=element_text(size=8))
gg <- gg + theme(axis.text.y=element_text(size=8, vjust=c(0, 0.5, 0.5, 0.5, 1)))
gg
即使在评论回复之后,它仍然不清楚你在追求的是什么。
gg <- ggplot(df)
gg <- gg + geom_segment(aes(x=Year, y=count, xend=Year, yend=0, color=Year), show.legend=FALSE)
gg <- gg + geom_point(aes(x=Year, y=count, color=Year))
gg <- gg + geom_text(aes(label=count, x=Year, y=count+40), vjust=0, size=3)
gg <- gg + scale_y_continuous(expand=c(0,0), limits=c(0, 800))
gg <- gg + ggthemes::scale_color_tableau(name="")
gg <- gg + facet_wrap(~Affiliation, nrow=1, scales="free_x")
gg <- gg + labs(x=NULL, y=NULL)
gg <- gg + theme_bw()
gg <- gg + theme(strip.background=element_blank())
gg <- gg + theme(strip.text=element_text(hjust=0))
gg <- gg + theme(panel.grid.major.x=element_blank())
gg <- gg + theme(panel.grid.minor.y=element_blank())
gg <- gg + theme(axis.ticks=element_blank())
gg <- gg + theme(axis.text.x=element_text(size=8))
gg <- gg + theme(axis.text.y=element_text(size=8, vjust=c(0, 0.5, 0.5, 0.5, 1)))
gg <- gg + theme(legend.key=element_blank())
gg <- gg + theme(legend.position="bottom")
gg