ggplot2,用于变量审美映射的线堆叠顺序 [英] ggplot2, line stacking order for aesthetic mapping of variable

查看:167
本文介绍了ggplot2,用于变量审美映射的线堆叠顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  library(foreach)$我有这种情况,我正在绘制一系列的行,如下所示: b $ b library(ggplot2)

df.test< - foreach(ix = seq(0,1,by = 0.01),combine = rbind)%do%{
theta = seq(-pi,pi,by = pi / 100)
data.frame(IX = ix,theta = theta,value = ix * sin(theta))
}
ggplot df.test,aes(x = theta,y = value,color = IX,group = IX,order = -IX))+ geom_path()+
theme_bw()+ labs(x =Theta,y =Sin(Theta),color =Index,title =Example Plot)+
theme(legend.position = c(0,1),legend.justification = c(0,1))

产生如下的情节:



如果您仔细观察原点,高指数值(浅蓝色)的值会堆积在线条的顶部低指数值(深蓝色)。



我该如何反转这使得深蓝线(低指数)堆积在浅蓝色线条(高指数)的顶部。

以上是类似于我的实际问题,我正在绘制各种温度的科学数据。低温值比高温值更有意义,所以我不希望低温线可能被高温线遮挡,实际上相反是我的偏好。

我相信颠倒组顺序应该做到这一点(不改变颜色),

  ggplot(df.test,aes(x = theta,y = value,color = IX,group = rev(IX)))+ 
geom_path(lwd = 2)+
theme_bw ()+
labs(x =Theta,y =Sin(Theta),color =Index,title =Example Plot)+
theme(legend.position = c 0,1),legend.justification = c(0,1))+
coord_cartesian(xlim = c(-0.2,0.2),ylim = c(-0.2,0.2))


I have this situation where I am plotting a series of lines, something like the following:

library(foreach)
library(ggplot2)

df.test <- foreach(ix = seq(0,1,by=0.01),.combine=rbind) %do% {
  theta = seq(-pi,pi,by=pi/100)
  data.frame(IX=ix,theta=theta,value=ix*sin(theta))
}
ggplot(df.test,aes(x=theta,y=value,color=IX,group=IX,order=-IX)) + geom_path() +
  theme_bw() + labs(x="Theta",y="Sin(Theta)",color="Index",title="Example Plot") +
  theme(legend.position=c(0,1),legend.justification=c(0,1))

Which produces a plot as follows:

If you look closely at the origin, the values of high index value (light blue) are stacked on top of the lines with low index value (dark blue).

How can I reverse this, so that the dark blue lines (low index) are stacked on top of the light blue lines (high index).

The above is analogous to my actual problem, where I am plotting scientific data for various temperatures. The low temperature values are more meaningful than the high temperature values, so I do not want the low temperature lines to be potentially masked by the high temperature lines, in fact the opposite is my preference.

解决方案

I believe reversing the group order should do it (without changing the colours),

ggplot(df.test,aes(x=theta,y=value,color=IX,group=rev(IX))) + 
    geom_path(lwd=2) +
    theme_bw() + 
    labs(x="Theta",y="Sin(Theta)",color="Index",title="Example Plot") +
    theme(legend.position=c(0,1),legend.justification=c(0,1)) + 
    coord_cartesian(xlim=c(-0.2,0.2), ylim=c(-0.2,0.2))

这篇关于ggplot2,用于变量审美映射的线堆叠顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆