ggplot2 geom_bar plot标签落在情节之外

时间:2013-08-03 02:39:19

标签: r ggplot2 bar-chart

问题

我正在尝试使用ggplot2在条形图上找到条形图的标签。 我得到的问题是标签落在情节“之外”。

加载包

library(ggplot2)
library(plyr)
library(reshape2)

Barplot

df_1 <- data.frame(PROV = c("BUENOS AIRES", "BUENOS AIRES", "BUENOS AIRES"),
                   variable = c("Var1", "Var2", "Var3"), 
                   value = c(15, 20, 5))

col_bar <- c("#00BA38", "#00BFC4", "#D7BA00")

z = ggplot(df_1, aes(x = factor(variable), y = value)) +
   geom_bar(data=df_1, stat = "identity", fill = col_bar) +
   geom_text(aes(label=value), colour= col_bar, size = 7, vjust = -1)

z = z + theme(panel.background = element_blank())
z = z + theme(panel.border = element_blank())
z = z + theme(panel.grid.major = element_blank())
z = z + theme(panel.grid.minor = element_blank())
z = z + theme(panel.grid.major = element_blank())
z = z + theme(panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())
z = z + theme(plot.background = element_blank())
z = z + theme(plot.background = element_blank())
z = z + theme(axis.text.y = element_blank())
z = z + theme(axis.ticks.y = element_blank())

print(z)

1 个答案:

答案 0 :(得分:0)

这似乎是前一个例子的一个很好的解决方案。 我会回答这个问题,留下参考资料。

z = ggplot(df_1, aes(x = factor(variable), y = value)) +
   geom_bar(data=df_1, stat = "identity", fill = col_bar) +
   geom_text(aes(label=value), colour= col_bar, size = 7, vjust = -1)

z = z + theme(panel.background = element_blank())
z = z + theme(panel.border = element_blank())
z = z + theme(panel.grid.major = element_blank())
z = z + theme(panel.grid.minor = element_blank())
z = z + theme(panel.grid.major = element_blank())
z = z + theme(panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())
z = z + theme(plot.background = element_blank())
z = z + theme(plot.background = element_blank())
z = z + theme(axis.text.y = element_blank())
z = z + theme(axis.ticks.y = element_blank())
z = z + coord_cartesian(ylim=c(0,23)) 
print(z)