在线图ggplot2的两侧设置空间

时间:2016-03-27 08:18:14

标签: r plot ggplot2 line

我在ggplot2图表的两侧设置空格时遇到问题。这是我开始的数据:

ade <- c(10,7,5,9,6,6,9,4,9,6,5,9,8,7,6,12,7,9,5,5)
adef<-cbind(c(2:21),c(ade/28))
colnames(adef)<-c("pos","f")
adef<-data.frame(adef)

当我非常简单地绘制它时,它看起来很好。

ggplot(data=adef, aes(x=pos, y=f)) +
+     ylim(0,1) + 
+     geom_line()

simple ggplot

但是,当我尝试更改刻度和标签的数量时,会发生意外情况:

ggplot(data=adef, aes(x=pos, y=f)) +
ylim(0,1) + 
 scale_x_discrete(breaks=c("2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22"),
                  labels=c("2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21",""))+
geom_line()

ggplot after setting scale_x_discrete

我想在右边添加一个空边距/空格,以便图形的左右部分看起来一样吗?

1 个答案:

答案 0 :(得分:2)

您不是在寻找scale_x_continuous吗?例如:

ggplot(data=adef, aes(x=pos, y=f)) +
  ylim(0,1) + 
  scale_x_continuous(breaks=2:22, labels=c(as.character(2:21), "")) +
  geom_line()

并且xlim()中的expand和/或scale_*()参数可能会有所帮助。