Reputation: 1
I'm trying to create a boxplot with jitter that shows the number of followers on the social networks in two different groups of people. I'm taking into consideration Facebook and Twitter. Each group has 50 names.
On the x axis I have Facebook and Twitter, each one with two boxes one next to each other (Group 1 and Group 2); on the y axis the number of followers. The problem is that if I count in the plot the number of points related to each box, they're not 50. So, something must be wrong.
Here is what I did.
I built a dataframe (called, say, my_df) in this way:
Basically, each name has two rows: one with the number of followers on Fb, one with the number of followers on Twitter.
Afterwards, I wrote this code:
my_df %>% ggplot(aes(Social_Net, Followers, fill = Group)) +
geom_boxplot() +
scale_y_continuous(trans="log2") +
geom_jitter(alpha = 0.2)
Anyone can tell me what mistake I made in building the dataframe and/or creating the boxplot?
Thank you in advance
Upvotes: 0
Views: 897
Reputation: 46958
When there's two groups, your boxplot has postion="dodge" (side by side). So when you do geom_jitter, you need to specify this "dodge" as well. In the latest version of ggplot2, you call position_jitterdodge()
with geom_point()
:
set.seed(100)
my_df = data.frame(
Followers=rpois(200,5000),
Social_Net=rep(c("Facebook","Twitter"),each=50,times=2),
Group=rep(c("Group1","Group2"),each=100)
)
ggplot(x,aes(Social_Net,Followers,fill=Group))+
geom_boxplot(alpha=0.1) +
geom_point(aes(col=Group),position=position_jitterdodge()) +
scale_y_continuous(trans="log2")
Upvotes: 3