R 条形图-棒棒糖图-可视化

R 条形图-棒棒糖图-可视化,r,ggplot2,bar-chart,R,Ggplot2,Bar Chart,我正在尝试使用下面的数据集创建条形图 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

我正在尝试使用下面的数据集创建条形图

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")
但是,我正在尝试基于以下示例创建此条形图的可视化:


这篇文章讨论过:

我仍然不能完全确定你的问题是什么。创建棒棒糖图表的示例没有使用
geom\u 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
#为什么要将此作为一个因素?

df$count我仍然不能完全确定你的问题是什么。创建棒棒糖图表的示例没有使用
geom\u 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
#为什么要将此作为一个因素?

df$count在哪里定义了
bgcolor
?这里的问题到底是什么?什么是
bgcolor
?把它砍下来,它就跑了。此外,如果在
ggplot
中设置
数据
参数,则不需要使用
$
-符号;您只需输入不带引号的变量名即可。@alistaire,谢谢alistaire,我使用
bgcolor
使背景透明。我现在已经把它去掉了。我同意您使用$Where is
bgcolor
定义?这里的问题到底是什么?什么是
bgcolor
?把它砍下来,它就跑了。此外,如果在
ggplot
中设置
数据
参数,则不需要使用
$
-符号;您只需输入不带引号的变量名即可。@alistaire,谢谢alistaire,我使用
bgcolor
使背景透明。我现在已经把它去掉了。我同意你的看法$