簡體   English   中英

ggplot2 geom_bar plot標簽位於圖之外

[英]ggplot2 geom_bar plot Labels fall outside plot

我正在嘗試使用ggplot2在條形圖上找到條形圖的標簽。 我得到的問題是標簽落在繪圖的“外面”。

加載包裹

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

條形圖

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)

對於前面的示例,這似乎是一個很好的解決方案。 我將回答這個問題以保留參考。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM