0

I'm trying to plot a geom_smooth line with ggplot2, but it doesn't appear. geom_point works fine, and I don't get any error messages.

 participantID token language measurement value 1 CC Teil German F2_0 906 2 DD Teil German F2_0 1638 3 FF Teil German F2_0 1781 4 FH Teil German F2_0 1195 5 GG Teil German F2_0 1796 6 HH Teil German F2_0 1695 

The 3 variables are: the measurement (N= 21: F2_0, F2_5, F2_10, ... , F2_95, F2_100), the value for each measurement and token (with two levels).

My ggplot line is ggplot(mydata,aes(x=measurement, y=value, color=token))+ geom_point()+ geom_smooth()+labs(title="F2 trajectories")

There are several threads on this issue already, but they don't seem to work for my data set. Any help would be very appreciated. Thanks.

1
  • 1
    It looks like your x variable is categorical. Given that, what should the relationship look like? Should the categorical variable be treated as ordinal or numeric or something else? That may help you figure out if a smooth is a., reasonable and b., doable. I searched "geom_smooth categorical" and this answer may also prove useful to you. Commented Sep 7, 2020 at 18:01

1 Answer 1

2

I would suggest next approach. You are using as x-axis a non numeric value that is why geom_smooth() is not working. I would suggest creating a reference value for x-axis and then use facets for measurement. Here the code:

library(ggplot2) library(dplyr) #Id variable and plot df1 %>% group_by(measurement) %>% mutate(id=1:n()) %>% ggplot(aes(x=id, y=value, color=token,group=measurement))+ geom_point()+ geom_smooth()+labs(title="F2 trajectories")+ facet_wrap(.~measurement) 

Output:

enter image description here

Some data used:

#Data df1 <- structure(list(participantID = c("CC", "DD", "FF", "FH", "GG", "HH"), token = c("Teil", "Teil", "Teil", "Teil", "Teil", "Teil" ), language = c("German", "German", "German", "German", "German", "German"), measurement = c("F2_0", "F2_0", "F2_0", "F2_0", "F2_0", "F2_0"), value = c(906L, 1638L, 1781L, 1195L, 1796L, 1695L)), class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6")) 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! it worked by adding group=token in the aes() of the geom_smooth

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.