假设有一个包含以下数据的数据框:
Freq=c(0.15,0.25,0.6)
x=as.factor(c("Some long long long long text", "Another long long long long text", "But, this is really long long long long long text" ))
df<-data.frame(x=x, Freq=Freq)
从这个数据框架中我想制作一个特定的情节,例如:
ggplot(df, aes(x=x, y=Freq)) +
geom_bar(stat="Identity", fill="dodgerblue3", width=.7) +
geom_text(aes(y = Freq, label = scales::percent(Freq), vjust = -0.5))+
scale_y_continuous(labels=scales::percent, name ="", limits = c(0,0.7), expand = c(0,0))+
scale_x_discrete(labels = levels(df$x), name="")
正如您所看到的,问题是我的离散变量x的标签因为太长而不能很好地显示。
我能想到的一个非常特别的解决方案是:
ggplot(df, aes(x=x, y=Freq)) +
geom_bar(stat="Identity", fill="dodgerblue3", width=.7) +
geom_text(aes(y = Freq, label = scales::percent(Freq), vjust = -0.5))+
scale_y_continuous(labels=scales::percent, name ="", limits = c(0,0.7), expand = c(0,0))+
scale_x_discrete(labels = c("Some long \n long long \n long text", "Another long \n long long \n long text", "But, this is \n really long long \n long long \n long text"), name="")
这给了我这个数字:
所以,我的问题是,是否有另一种解决方案不像我那样需要手动干预标签。