简体   繁体   中英

Connecting points with a line in scatterplot using ggplot2

I have a scatterplot that breaks the points out into different colors by the category. I want one of these categories to have a line connecting the dots to highlight this category's data. I'm having trouble figuring this out...

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)))

The ftheme code is just colors and formatting. Any ideas? I've tried adding geom_line(aes(linetype=floyd[Opponent="Manny Pacquiao"]), size=1) but it errors out with

Error in [.data.frame`(floyd, Opponent = "Manny Pacquiao") :    unused argument (Opponent = "Manny Pacquiao")

EDIT: I've updated the code above to exclude ftheme so it's reproducable. Please see the sample dataset from with three categories. I just want any one of these to have connected points: http://goo.gl/3c3vBU

I can't give a tailored answer without being able to run your code on a sample of your data, but you can use scale_color_manual to set the colour of the category you want to highlight to, say, "red" and set all the others 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 plot code:

scale_colour_manual(values=c(NA, "red", rep(NA,3)))

If you have points that are tied to the color aesthetic as well, then you'll need to change the points to a fill aesthetic (eg, fill=Opponent ) and use a filled point marker that you can set manually using shape or pch . Otherwise, your point markers will disappear along with the lines. Marker numbers 21 through 25 are filled (see ?pch for more on point markers).

UPDATE: Here's my attempt using the data you provided. I'm not exactly sure how you want the legends and other details to look, so let me know if this works. I've switched to ggplot , as I don't know the ins and outs of 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))) 

在此输入图像描述

Try to add:

 geom_line(data=subset(floyd,Opponent=="Manny Pacquiao"), aes(factor(Round), Opp.Landed, group=Opponent), size = 2)

在此输入图像描述

Very easy piece of code which makes a subset of your data of which gives a nice line of size 2 through your data points.

(for the image I used the opponent Miguel Cotto since you did not provide Manny Pacquiao in the data set)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM