Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 interaction plot error

Tags:

r

ggplot2

I am trying to create an interaction plot, and R is throwing the error geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?, which I don't understand why. Below is my data frame:

       topPagesCount DIRTY_INDUSTRY IND_DIRTY_HETEROGENEITY
1             10      1.4444444               1.1727001
2            831      1.4444444               1.1727001
3              1      0.8218391               0.4599108
4              0      0.8218391               0.4599108
5              0      0.8821549               0.4870270
6             30      0.8190476               0.6582197
7             26      0.8218391               0.4599108
8              0      1.4444444               1.1727001
9              7      0.8821549               0.4870270
10           398      0.8218391               0.4599108

and below is my code:

greatDF$DIRTY_INDUSTRY_fac <- as.factor (greatDF$DIRTY_INDUSTRY)
ggplot(data = greatDF, aes(x = IND_DIRTY_HETEROGENEITY, y=topPagesCount,
colour=DIRTY_INDUSTRY_fac, group=DIRTY_INDUSTRY_fac))+
stat_summary(fun.y=mean, geom="point")+
stat_summary(fun.y=mean, geom="line")

I don't see any reason for the error because clearly, there are more than 1 type of value for my response variable topPagesCount for the interaction term DIRTY_INDUSTRY:IND_DIRTY_HETEROGNEITY...am I right? maybe I am misunderstanding something...

thanks,

like image 565
Jin-Dominique Avatar asked Feb 07 '26 13:02

Jin-Dominique


1 Answers

The reason this happens, as @Troy points out, is because the grouping itself is meaningless for geom_line() or geom_path(). There are no points to be connected with lines at all!

That's why everything is OK when you remove the last line. Note that this "error" is not an actual error, it plots the legend as it is intended to look, there is not a single actual line that should be plotted according to your aesthetics and stats.

How to fix this? Well, that depends on what you are trying to achieve, as usual. Note the difference between your code and mine:

ggplot(data = greatDF, aes(x = IND_DIRTY_HETEROGENEITY, y=topPagesCount,
                       colour=DIRTY_INDUSTRY_fac, group=DIRTY_INDUSTRY_fac)) +
geom_line(size=1.4) + 
geom_point(size=5, shape=10) + 
stat_summary(fun.y=mean, geom="point", size=5)

Is my guess correct? You may see this question for more insights on the topic.

enter image description here

like image 64
tonytonov Avatar answered Feb 09 '26 03:02

tonytonov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!