简体   繁体   English

R-贴合X轴标签

[英]R - Fitting X-Axis Labels

I am currently making a bar plot using ggplot2, however, the x-axis labels are very squished together and I cannot read them. 我目前正在使用ggplot2制作条形图,但是,x轴标签被挤压在一起,无法读取。 I have read other examples of people tilting these at a 45 degree angle, which is definitely one option. 我还读过其他一些人以45度角倾斜这些示例,这绝对是一个选择。 I tried this to no avail with the code below: 我用下面的代码尝试了一下:

ggplot(data = Overall_survival, 
       aes(x=reorder(Name, -OS), y=OS, fill = factor(Best_Response))) + 
  ylim(0, 50) + 
  ylab("Overall Survival (months)") + 
  xlab("Response by Name") + 
  theme_classic() + 
  ggtitle("Overall Survival in Patients Treated with Treatment (n=35)") + 
  theme(plot.title = element_text(hjust = 0.5)) + 
  geom_bar(stat="identity", width = 0.6) + 
  guides(fill = guide_legend(title = "Best Response"))

Should I tilt the labels so that the "Name" fits beneath each bar, or is there a way to decrease the font size so that it fits horizontal? 我应该倾斜标签以使“名称”适合每个栏的下方,还是可以减小字体大小以使其适合水平?

You can play around yourself, but you access the elements of each plot via theme : 您可以自己玩耍,但是可以通过theme访问每个剧情的元素:

ggplot(...) +
... +
theme(axis.text.x = element_text(size = 20, angle = 45, hjust = 1))

You can substitute text for axis.text.x or axis.text.y , etc.. 您可以将text替换为axis.text.xaxis.text.y等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM