我在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()
但是,当我尝试更改刻度和标签的数量时,会发生意外情况:
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()
我想在右边添加一个空边距/空格,以便图形的左右部分看起来一样吗?
答案 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_*()
参数可能会有所帮助。