March 16, 2017 at 6:36 pm EDT #19530
I ran this code along with the video, but instead of red and blue collars, I got “b” for democratic and “r” for republicans. I think I did it right…any ideas?
ggplot(house_votes_Dem, aes(x=aye, y=nay, shape= partycluster.dem)) +
ggtitle(“Aye vs Nay Dem Votes”) +
xlab(“Aye Votes”) +
ylab(“Nay Votes”) +
labels= c(“Democrat”, “Republican”),
values= c(“blue”, “red”)) +
theme_light()March 17, 2017 at 11:48 am EDT #19536
Thank you for your question! So here is the code for the graph I think you’re trying to create (from slide 65):
ggplot(house_votes_Dem, aes(x = yea, y = nay, color = party_labels_Dem, #<- tell R how to color # the data points shape = party_clusters_Dem)) + geom_point(size = 6) + ggtitle("Aye vs. Nay votes for Democrat-introduced bills") + xlab("Number of Aye Votes") + ylab("Number of Nay Votes") + scale_shape_manual(name = "Cluster", labels = c("Cluster 1", "Cluster 2"), values = c("1", "2")) + scale_color_manual(name = "Party", #<- tell R which colors to use and # which labels to include in the legend labels = c("Democratic", "Republican"), values = c("blue", "red")) + theme_light()
There are a few things you may notice here. The first is that there is a ‘color’ argument in the aes() layer at the beginning of the code, which will allow the graph to color particular points. So make sure to include that in your aesthetics layer. The second aspect I wanted to point out is that your scale_shape_manual is telling R to use the letters ‘b’ and ‘r’ (in the values argument) as the shape. I think you want to use the values of 1 and 2 to get those two shapes to correspond to the cluster labels of 1 and 2. You can then add the scale_color_manual() argument to specify the colors you want for the two different political parties.
Ggplot2 can always be a bit tough to get the hang of – I always like to think of ggplot2 as a graph with layers on it. So the aesthetics layer is the bottom layer that defines the data we want on there, and then we add layers on it like the labels, shapes, colors, legends, etc.
Please let me know if this helps!
MeravMarch 17, 2017 at 7:44 pm EDT #19537
Thank you so much! can’t believe I missed that …grrrr….
You must be logged in to reply to this topic.