Connecting points to a line in a scatter plot using ggplot2
I have a scatterplot that breaks down points into different colors by category. I want one of these categories to have a line connecting the dots to highlight the data for that category. It's hard for me to understand this ...
Round <- read.csv("http://goo.gl/3c3vBU") # Data
qplot(factor(Round), Opp.Landed, data=floyd, color=Opponent, size=Opp.Percent.Landed, alpha = I(0.7)) +
labs(x="Round", y="Punches Landed", title="Opponent Punches Landed / Percentage", colour="Boxer", size="Connect Percentage") +
scale_linetype_manual(values=1:2, labels=c("Boxer", "Connect Percentage")) +
guides(colour = guide_legend(override.aes = list(size=5)))
Code ftheme
is just colors and formatting. Any ideas? I tried to add geom_line(aes(linetype=floyd[Opponent="Manny Pacquiao"]), size=1)
, but with error
Error in [.data.frame`(floyd, Opponent = "Manny Pacquiao") : unused argument (Opponent = "Manny Pacquiao")
EDIT: I updated the code above to exclude ftheme so that it is reproducible. Please see Sample Data from Three Categories. I just want one of them to have associated points: http://goo.gl/3c3vBU
source to share
Try adding:
geom_line(data=subset(floyd,Opponent=="Manny Pacquiao"), aes(factor(Round), Opp.Landed, group=Opponent), size = 2)
A very simple piece of code that makes a subset of your data that gives a nice size 2 string across your data points.
(for the image I used Miguel Cotto's adversary, since you did not provide Manny Pacquiao in the dataset)
source to share
I can't give a personalized answer without being able to run the code over a sample of your data, but you can use scale_color_manual
to set the color of the category you want to highlight, like "red", and set everyone else to NA
. For example, if the category you want to highlight is the second category and you have a total of five categories, then add this to your graph code:
scale_colour_manual(values=c(NA, "red", rep(NA,3)))
If you have glasses tied to a color aesthetic, then you will need to change dots to fill aesthetics (for example fill=Opponent
) and use a filled dot marker that you can manually set using shape
or pch
. Otherwise, the point markers will disappear along with the lines. Marker numbers 21 through 25 are filled in (see ?pch
for more information on point markers).
UPDATE: Here's my attempt at using the data you provided. I'm not really sure how you want the legends and other details to look, so let me know if that works. I switched to ggplot
since I don't know how the inputs and outputs are qplot
.
ggplot(floyd, aes(factor(Round), Opp.Landed, color=Opponent,
fill=Opponent, group=Opponent, size=Opp.Percent.Landed),
alpha = 0.7, pch=21) +
geom_point(pch=21, colour=NA) +
geom_line() +
labs(x="Round", y="Punches Landed", title="Opponent Punches Landed / Percentage",
colour="Boxer", size="Connect Percentage") +
scale_linetype_manual(values=1:2, labels=c("Boxer", "Connect Percentage")) +
scale_colour_manual(values=c(hcl(15,100,65), NA, NA), guide="none") +
guides(fill = guide_legend(override.aes = list(size=5)))
source to share