简体   繁体   English

更改 R / ggplot2 中点的绘制顺序

[英]Changing plotting order of points in R / ggplot2

I have the following code to plot a large dataset (450k) in ggplot2我有以下代码到 plot ggplot2 中的一个大数据集(450k)

x<-ggplot()+
  geom_point(data=data_Male,aes(x=a,y=b),color="Turquoise",position=position_jitter(w=0.2,h=1),alpha=0.1,size=.5,show.legend=TRUE)+
  geom_point(data=data_Female,aes(x=a,y=b),color="#FF9999",position=position_jitter(w=0.2,h=1),alpha=0.1,size=.5,show.legend=TRUE)+
  theme_bw()
x<-x+geom_smooth(data=data_Male,aes(x=a,y=b,alpha="Male"),method="lm",colour="Blue",linetype=1,se=T)+
  geom_smooth(data=data_Female,aes(x=a,y=b,alpha="Female"),method="lm",colour="Dark Red",linetype=5,se=T)+
  geom_smooth(data=data_All,aes(x=a,y=b,alpha="All"),method="lm",colour="Black",linetype=3,se=T)+
  scale_fill_discrete(name="Key",labels=c("Female","Male","All"))+
  scale_colour_discrete(name="Plot Colour",labels=c("Female","Male","All"))+
  scale_alpha_manual(name="Key",
                     values=c(1,1,1),
                     breaks=c("Female","Male","All"),
                     guide=guide_legend(override.aes=list(linetype=c(5,1,3),name="Key",
                                                          shape=c(16,16,NA),
                                                          color=c("Dark Red","Blue","Black"),
                                                          fill=c("#FF9999","Turquoise",NA))))

How can I change the order in which points are plotted?如何更改绘制点的顺序? I have seen answered questions here dealing with a single dataframe but I am working with several dataframes so I cannot re-order the rows or ask ggplot to plot by certain criteria from within the dataframe. You can see an example of the kind of problem that this causes in the attached picture: the Female points are plotted on top of the Male points.我在这里看到了处理单个 dataframe 的已回答问题,但我正在处理多个数据框,因此我无法重新排序行或根据 dataframe 中的某些标准向 ggplot 询问 plot。您可以看到此类问题的示例这导致在附图中:女性点被绘制在男性点之上。 Ideally I would like to be able to plot all the points in a random order, so that one "cloud" of points is not plotted on top of the other, obscuring it (NB the image shown doesn't include the "All" line).理想情况下,我希望能够以随机顺序对所有点进行 plot,这样一个点的“云”就不会绘制在另一个点的顶部,从而掩盖它(注意显示的图像不包括“全部”线).

Any help would be appreciated.任何帮助,将不胜感激。 Thank you.谢谢你。在此处输入图像描述

I belive this is not possible.我相信这是不可能的。 The following should work though:以下应该可以工作:

You'd have to paste the two data frames together to df .您必须将两个数据框一起粘贴到df The new data frame will appear sorted by male and female.新数据框将按男性和女性排序。

You can then suffle the new data frame:然后您可以对新数据框进行 suffle:

set.seed(42)
rows <- sample(nrow(df))
male_female_mixed <- df[rows, ]

Then you can plot male_female_mixed那你可以plot male_female_mixed

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

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