Reputation: 53
sorry for the poor header, but I was struggling to find the right wording. I hope you understand my question.
I would like to create a chart where I first draw all red elements (both lines and points) and then all all elements blue elements (both lines and points). In other words first draw everything for group==1 and then all elements for group==2. I've achieved what I want with fig2 below, but it just feels very inefficient. Is there a better way to do it?
Thank you, Hans
# Load library
library("tidyverse")
library("patchwork")
# Some data
df<-tibble(x=c(1,2,5,3,4,4),y=c(1,2,4,5,3,6),group=c(1,1,1,2,2,2))
# Plot
fig1<-ggplot(df,aes(x=x,y=y,color=as.factor(group)))+
geom_point(size=20)+
geom_line(size=2)+
theme(legend.position = "top")+labs(title="Not what I want")
# Manual
fig2<-
ggplot(df,aes(x=x,y=y,color=as.factor(group)))+
geom_point(,size=20)+
geom_line(size=2)+
geom_point(df%>%filter(group==1),mapping=aes(x=x,y=y,color=as.factor(group)),size=20, color="#F8766D")+
geom_line(df%>%filter(group==1),mapping=aes(x=x,y=y,color=as.factor(group)),size=2, color="#F8766D")+
geom_point(df%>%filter(group==2),mapping=aes(x=x,y=y,color=as.factor(group)),size=20, color="#00BFC4")+
geom_line(df%>%filter(group==2),mapping=aes(x=x,y=y,color=as.factor(group)),size=2, color="#00BFC4")+
theme(legend.position = "top")+labs(title="What I want")
fig2
fig1+fig2
Upvotes: 2
Views: 53
Reputation: 410
It may look silly, but I just changed the order between line
and point
.
fig2<-ggplot(df,aes(x=x,y=y,color=as.factor(group)))+
geom_line(size=2)+
geom_point(size=25)+
theme(legend.position = "top")+labs(title="Is this what you want?")
plot(fig2)
another attempt:
df<-tibble(x=c(1,2,4,3,4,4),y=c(1,2,4,5,3,6),group=c(1,1,1,2,2,2))
df$group<-as.factor(df$group)
p1<-ggplot(df,aes(x,y))+geom_point(aes(colour=group),size=25)+geom_line(aes(colour=group),size=2)+theme(legend.position = "top")+labs(title="Is this what you want?")
plot(p1)
Upvotes: 1