繁体   English   中英

改变ggplot2中的Y轴断点

[英]Changing Y-axis breaks in ggplot2

我有这个代码给出如下图:

d=ggplot(df, aes(x=Year, y=NAO_Index, width=.8)) +
+ geom_bar(stat="identity", aes(fill=NAO_Index>0), position='identity', col = 'transparent') +
+ theme_bw() + scale_fill_manual(values=c("royalblue", "firebrick3"), name="NAO Oscillation", labels=c("Negative", "Positive"), guide=guide_legend(reverse=TRUE)) +
theme(legend.position=c(0.06, 0.92)) + 
+ theme(axis.title.x=element_text(vjust=-0.2)) +
+ geom_line(data=dfmoveav, aes(x=Year ,y=moveav)) +
+ ylab("NAO Index") +
+ ggtitle("NAO Index between 1860 and 2050") +
+ scale_x_continuous(breaks=c(seq(1860,2050,10))) +
+ scale_y_continuous(breaks=c(seq(-3.5,3.5,0.5)))

我只关心最后一行。 在图中,y轴仅从-3到2.5。 我如何从-3.5到3.5,所以它是均匀的?

我确定我正在制作一个简单的错误,但无法弄清楚!

在此输入图像描述 提前谢谢了。

你快到了。 尝试设置限制。

d=ggplot(df, aes(x=Year, y=NAO_Index, width=.8)) +
+ geom_bar(stat="identity", aes(fill=NAO_Index>0), position='identity', col = 'transparent') +
+ theme_bw() + scale_fill_manual(values=c("royalblue", "firebrick3"), name="NAO Oscillation", labels=c("Negative", "Positive"), guide=guide_legend(reverse=TRUE)) +
theme(legend.position=c(0.06, 0.92)) + 
+ theme(axis.title.x=element_text(vjust=-0.2)) +
+ geom_line(data=dfmoveav, aes(x=Year ,y=moveav)) +
+ ylab("NAO Index") +
+ ggtitle("NAO Index between 1860 and 2050") +
+ scale_x_continuous(breaks=c(seq(1860,2050,10))) +
+ scale_y_continuous(breaks=c(seq(-3.5,3.5,0.5)), limits = c(-3.5, 3.5))

更多关于它的信息

要将线条映射到图例中,您应该将变量映射到美学。 但这不是微不足道的,你会找到避免这种方法的参考。

df <- data.frame(year=factor(seq(1:10)),
                 nao = rnorm(10, 0, 2),
                 mov = rnorm(10, 0,3))
df2 <- data.frame(year=factor(seq(1:10)),
                  mov = df$nao+rnorm(10, 0, 0.1),
                  g = .1)
ggplot() +
  geom_bar(data = df, aes(x=year, y=nao, fill=nao > 0), width=.8, 
           stat="identity", position ='identity', col = 'transparent') +
  geom_line(data = df2, aes(x = year, y = mov, group = g, size = g)) +
  scale_fill_manual(values=c("royalblue", "firebrick3"),
                    name="NAO Oscillation",
                    labels=c("Negative", "Positive"),
                    guide=guide_legend(reverse=TRUE)) +
  scale_size('Trend', range = 1, labels = 'Moving\naverage') +
  ggtitle("NAO Index between 1860 and 2050") +
  scale_y_continuous(breaks=c(seq(-5,5,0.5)), limits = c(-5, 5)) +
  ylab("NAO Index") +
  theme(legend.position = c(0.07, 0.80),
        axis.title.x = element_text(vjust= -0.2),
        legend.background = element_blank())

ggplot吧和线

这可能不是将变量映射到美学的最佳方法。

暂无
暂无

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

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