x轴刻度标签不适合在Rstudio中使用ggplot2

时间:2017-10-16 04:08:55

标签: r ggplot2 rstudio

我正在尝试使用Rstudio中的ggplot2使用对数刻度制作图表,并且最后的x轴刻度标签将不适合图表。

这是我的绘图代码

ggplot(data=user.counts, aes(x=counter, y=Number_obs)) +
geom_line()+
scale_y_log10(breaks=c(1,10,100,1000,10000),labels=c(1,10,100,1000,10000))+
scale_x_log10(breaks=c(1,10,100,1000,10000),labels=c(1,10,100,1000,10000))+
labs(x="No. observers",y="No. observations",title="")+
theme_bw(base_size = 20)

这是我图表的截图:

graph

我已经尝试在RStudio中制作更大的绘图窗格并弹出绘图窗格,这两种方法都没有什么区别。

非常感谢

1 个答案:

答案 0 :(得分:0)

选项1 。右对齐x轴标签:

ggplot(data=user.counts, aes(x=counter, y=Number_obs)) +
  geom_line()+
  scale_y_log10(breaks=c(1,10,100,1000,10000), labels=c(1,10,100,1000,10000))+
  scale_x_log10(breaks=c(1,10,100,1000,10000), labels=c(1,10,100,1000,10000))+
  labs(x="No. observers",y="No. observations",title="")+
  theme_bw(base_size = 20) +
  theme(axis.text.x = element_text(hjust = 1)) # default is hjust = 0.5

plot1

选项2 。增加扩张常数:

ggplot(data=user.counts, aes(x=counter, y=Number_obs)) +
  geom_line()+
  scale_y_log10(breaks=c(1,10,100,1000,10000), labels=c(1,10,100,1000,10000))+
  scale_x_log10(breaks=c(1,10,100,1000,10000), labels=c(1,10,100,1000,10000),
                expand = c(0.1, 0))+ # default is c(0.05, 0) for continuous variable
  labs(x="No. observers", y="No. observations", title="")+
  theme_bw(base_size = 20)

plot2

使用的样本数据:

set.seed(1)
user.counts <- data.frame(
  counter = seq(1, 1000),
  Number_obs = seq(1000, 1) * 10 + rnorm(1000)
)