ggplot2 geom_bar plot标签位于图之外 [英] ggplot2 geom_bar plot Labels fall outside plot

查看:106
本文介绍了ggplot2 geom_bar plot标签位于图之外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ggplot2在条形图上找到条形图的标签.我得到的问题是标签落在绘图的外面".

I'm trying to locate labels of a bar plot using ggplot2 over the bars. The problem I get is that the labels fall "outside" the plot.

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)

推荐答案

对于前面的示例,这似乎是一个很好的解决方案.我会回答这个问题,以留下参考.

This seems to be a good solution for the previous example. I'll answer the question to leave the reference.

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)

这篇关于ggplot2 geom_bar plot标签位于图之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆