Home > Net >  Generate a legend and connecting data points (ggplot2)
Generate a legend and connecting data points (ggplot2)

Time:03-16

I am pretty new to R. I've been trying (for the entire day) to plot data points on one graph and generate a legend for it.

I have a raw data set about people's political ideology (factor, 1=liberal, 2=conservative, 3=neutral), firm choice (liberal or conservative), and logarithm value of firm wage difference (liberal firm wage - conservative firm wage).

What I tried to do is

  • Divide the people-tasks into quartiles according to the difference in log offered wages.
  • Divide each quartile by people's political ideology (3 types). This gave me 4 X 3 groups.
  • Plot 12 data points in one graph.

I wanted to

  • plot data points in one graph,
  • generate a legend, and
  • connect dots based on the political ideology, respectively.

So, I computed 12 data points (4 for liberal, 4 for neutral, 4 for conservative) and plot them using ggplot2. I used blue color for liberal, green color for neutral, and red color for conservative.

myggplot <- ggplot(, aes(x=c(-0.6, 0.55), y=c(0,1)))  
  geom_point(aes(x=-0.4384035, y=0.3995726),col = "blue",shape = 15, size = 3)  
  annotate("point", x=-0.221052, y=0.4463519, col="blue", shape=15, size=3) 
  annotate("point", x=0.0839785, y=0.9610656, col="blue", shape=15, size=3) 
  annotate("point", x=0.4146425, y=0.9598309, col="blue", shape=15, size=3) 
  annotate("point", x=-0.4384035, y=0.1650485, col="green", shape=17, size=3) 
  annotate("point", x=-0.221052, y=0.25, col="green", shape=17, size=3) 
  annotate("point", x=0.0839785, y=0.8275862, col="green", shape=17, size=3) 
  annotate("point", x=0.4146425, y=0.8152174, col="green", shape=17, size=3) 
  annotate("point", x=-0.4384035, y=0.06818182, col="red", shape=16, size=3) 
  annotate("point", x=-0.221052, y=0.08527132, col="red", shape=16, size=3) 
  annotate("point", x=0.0839785, y=0.6377953, col="red", shape=16, size=3) 
  annotate("point", x=0.4146425, y=0.7080292, col="red", shape=16, size=3) 
  scale_color_manual(name="Political ideology",
                     values=c("Liberal"="blue", "Neutral"="green", "Conservative"="red"),
                     labels=c("Liberal", "Neutral", "Conservative"),
                     guide="legend") 
  scale_shape_identity()  
  labs(y="Probability of Choosing a Liberal Firm", x="Log Wage (Liberal Firm - Conservative Firm)",
       title="The Effects of Log Wage Difference on Firm Choice")  
  theme(
    plot.title = element_text(size=10, hjust=0.5, face="bold"),
    axis.title.x = element_text(size=10),
    axis.title.y = element_text(size=10)
  )

However, I cannot generate a legend even after I tried several codes. This is what I can see:

ideology

CodePudding user response:

It can be done by modifying your code slightly, but as has already been mentioned it's not the best way.

legend_colors <- c("Liberal"="blue", "Neutral"="green", "Conservative"="red")
legend_shapes <- c("Liberal"=15, "Neutral"=17, "Conservative"=16)

myggplot <- ggplot(, aes(x=c(-0.6, 0.55), y=c(0,1)))  
  geom_point(aes(x=-0.4384035, y=0.3995726, col= "Liberal", shape="Liberal"), size = 3)  
  geom_point(aes(x=-0.221052, y=0.4463519, col="Liberal", shape="Liberal"), size=3)  
  geom_point(aes(x=0.0839785, y=0.9610656, col="Liberal", shape="Liberal"), size=3)  
  geom_point(aes(x=0.4146425, y=0.9598309, col="Liberal", shape="Liberal"), size=3)  
  geom_line(aes(x=c(-0.4384035,-0.221052,0.0839785,0.4146425),
                y=c(0.3995726,0.4463519,0.9610656,0.9598309),
                col = "Liberal"))  
  geom_point(aes(x=-0.4384035, y=0.1650485, col="Neutral", shape="Neutral"), size=3)  
  geom_point(aes(x=-0.221052, y=0.25, col="Neutral", shape="Neutral"), size=3)  
  geom_point(aes(x=0.0839785, y=0.8275862, col="Neutral", shape="Neutral"), size=3)  
  geom_point(aes(x=0.4146425, y=0.8152174, col="Neutral", shape="Neutral"), size=3)  
  geom_line(aes(x=c(-0.4384035,-0.221052,0.0839785,0.4146425),
                y=c(0.1650485,0.25,0.8275862,0.8152174),
                col = "Neutral"))  
  geom_point(aes(x=-0.4384035, y=0.06818182, col="Conservative", shape="Conservative"), size=3)  
  geom_point(aes(x=-0.221052, y=0.08527132, col="Conservative", shape="Conservative"), size=3)  
  geom_point(aes(x=0.0839785, y=0.6377953, col="Conservative", shape="Conservative"), size=3)  
  geom_point(aes(x=0.4146425, y=0.7080292, col="Conservative", shape="Conservative"), size=3)  
  geom_line(aes(x=c(-0.4384035,-0.221052,0.0839785,0.4146425),
                y=c(0.06818182,0.08527132,0.6377953,0.7080292),
                col = "Conservative"))  
  scale_color_manual(name="Political ideology",
                     values= legend_colors,
                     guide="legend")  
  scale_shape_manual(name="Political ideology",
                     values= legend_shapes,
                     guide="legend")  
  labs(y="Probability of Choosing a Liberal Firm", x="Log Wage (Liberal Firm - Conservative Firm)",
       title="The Effects of Log Wage Difference on Firm Choice")  
  theme(plot.title = element_text(size=10, hjust=0.5, face="bold"),
        axis.title.x = element_text(size=10),
        axis.title.y = element_text(size=10))


myggplot

enter image description here

  • Related