[英]r- ggplot2: polar line plot with 2 lines
Using ggplot2
, I am trying to graph two treatments in a polar line graph. 使用
ggplot2
,我试图在极线图中绘制两种处理方式。 I'm able to graph the points for both treatments and get a line for the first treatment that I input, but the line that I want to connect the second subset is just overlaying over the second. 我可以为两种治疗方法绘制点图,并获得我输入的第一种治疗方法的直线,但是我想要连接第二个子集的线仅覆盖第二种方法。
category highest lowest
1 A 71 23
2 B 81 38
3 C 77 22
4 D 83 56
5 E 84 32
6 F 82 55
7 G 73 26
So far, my code looks like this 到目前为止,我的代码看起来像这样
p1<-ggplot(data=d,aes(x=factor(category),y=highest,group=1)) +
ylim(0,NA) +
geom_point(color='purple',stat='identity')+
geom_polygon(color='purple',fill=NA)+
coord_polar(start =-pi* 1/7)
p1
p1 +
geom_point(aes(x=factor(category),y=lowest),color='green',stat='identity')+
geom_polygon(color='green',fill=NA)+
coord_polar(start =-pi* 1/7)
and its looking like this 它看起来像这样
Any help? 有什么帮助吗? Thanks!!
谢谢!!
You are still using the overall y aesthetic ("highest") in the second geom_polygon
call. 在第二个
geom_polygon
调用中,您仍在使用整体y美学(“最高”)。 To plot a different column, you'll need to define it in aes
within the layer. 要绘制不同的列,您需要在层中的
aes
定义它。
geom_polygon(aes(y = lowest), color='green',fill=NA)
The more standard way to do such a plot, though, is to get your dataset into a long format, where you grouping variable (highest and lowest) is a variable and all your y values are in a single column for plotting. 不过,进行这种绘图的更标准方法是将数据集转换为长格式,其中将变量(最高和最低)分组是一个变量,并且所有y值都位于一列中以进行绘图。
library(reshape2)
d2 = melt(d, id.var = "category")
Once you have that you can map color
to the grouping variable. 一旦有了,就可以将
color
映射到分组变量。 You set colors to specific values using scale_color_manual
. 您可以使用
scale_color_manual
将颜色设置为特定值。 You get a legend by default (which might be a good thing), but you an suppress it using guide = "none"
. 默认情况下,您会得到一个图例(这可能是一件好事),但是您可以使用
guide = "none"
来取消它。
ggplot(data=d2, aes(x=factor(category), y=value, group = variable, color = variable)) +
ylim(0,NA) +
geom_point(, stat='identity') +
geom_polygon(fill=NA)+
coord_polar(start =-pi* 1/7) +
scale_color_manual(values = c("purple", "green"), guide = "none")
Both approaches give the same plot in the end: 两种方法最终给出相同的图:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.