【发布时间】:2018-01-22 12:45:43
【问题描述】:
使用如下所示的数据框。
text <- "
name,var,value
tata_zest,a,99.8
toyota_prius,a,100.0
honda_civic,a,99.9
nissan_rx4,a,10
tata_zest,b,8
toyota_prius,b,7
honda_civic,b,11
nissan_rx4,b,13
tata_zest,c,0.2
toyota_prius,c,0.21
honda_civic,c,0.15
nissan_rx4,c,0.32
tata_zest,d,300
toyota_prius,d,400
honda_civic,d,200
nissan_rx4,d,650
"
df <- read.table(textConnection(text), sep=",", header = T, stringsAsFactors = F)
我正在使用 ggplot 创建一个带有标签的条形图,如下所示。
ggplot() +
geom_bar(
data=df, color = "white", stat = "identity", position='dodge',
aes(x=name, y=value)
) + coord_flip() +
geom_text(data = df, angle = 0, hjust = 1,
aes(x=name, y=value, label=value)
) +
facet_wrap(~ var, scales = "free", ncol = 2) +
theme(
axis.text.x=element_blank(),
axis.title.y=element_blank()
)
这给出了如下图。
我现在需要通过修剪标签中_ 之后的所有内容来替换 y 轴刻度线文本。我需要一种在 ggplot 中执行此操作的方法 - 而不是在原始数据帧 df 中。我希望我可以在 ggplot 中使用 gsub("[_].*$", "", x) 之类的东西 - 我该怎么做?
【问题讨论】: