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:
I also tried to draw three lines, each connecting 4 dots (of the same color) respectively using the following code but failed.
myggplot + geom_line(mapping=aes(colour="blue", "green", "red", size=1))
It gives me an error message:
Error: Discrete value supplied to continuous scale