lily23
lily23

Reputation: 79

Plotting a graph with lines one factor and data points only for another factor in R

I have made the following with ggplot2, using the code below:

graph

ggplot(data=d, aes(x=Characteristic, y=Rating, group=Factor, colour=Factor)) +
geom_point() +
geom_line()+
ylim(0,10)+
xlab("Characteristics") + theme(text = element_text(size=20))

I only want to use lines to connect data points for two of the Factors (A-H), the other factors just need the data points (no lines connecting). How can I do this?

My data is in this form:

     Factor Characteristic Rating
1       A             OA      7
2       B             OA      6
3       C             OA      5
4       D             OA      4
5       E             OA      5
6       F             OA      6
7       G             OA      7
8       H             OA      1
9       A             HS      7
10      B             HS      2
11      C             HS      5

Upvotes: 0

Views: 1503

Answers (1)

Joel Carlson
Joel Carlson

Reputation: 640

Add an identifier into your data frame with ifelse, and filter your data with dplyr:

d$lc <- ifelse(d$Factor %in% c("A", "H"), "line", "point")

ggplot(data=d, aes(x=Characteristic, y=Rating, group=Factor, colour=Factor)) +
geom_point() +
geom_line(data=filter(d, d$lc == "line"), aes(x=Characteristic, y=Rating, group=Factor, colour=Factor))

Should do the trick.

Upvotes: 3

Related Questions