- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a plot for the response variable grouped by two columns using ggplot2 in R?
When two categorical variables make an impact on the response variable together then it is necessary to visualize their effect graphically because this graph helps us to understand the variation in the effect. Therefore, we can create a plot for the response variable that changes with one or both of the categorical independent variables. This can be done with the help of using interaction function in ggplot2.
Consider the below data frame −
> set.seed(1) > y<-rpois(30,2) > Group1<-rep(c(letters[1:5]),times=6) > Group2<-rep(c("Ph1","Ph2","Ph3"),each=10) > df<-data.frame(y,Group1,Group2) > head(df,20) y Group1 Group2 1 1 a Ph1 2 1 b Ph1 3 2 c Ph1 4 4 d Ph1 5 1 e Ph1 6 4 a Ph1 7 4 b Ph1 8 2 c Ph1 9 2 d Ph1 10 0 e Ph1 11 1 a Ph2 12 1 b Ph2 13 3 c Ph2 14 1 d Ph2 15 3 e Ph2 16 2 a Ph2 17 3 b Ph2 18 6 c Ph2 19 1 d Ph2 20 3 e Ph2
Loading ggplot2 package −
Creating the plot with interaction
> qplot(Group1, y, data=df, group=Group2, color=Group2, geom='line')+ + geom_smooth(aes(group=interaction(Group1, Group2))) `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Here, we can see that all the lines are crossing each other therefore there exists an interaction effect of independent variables, which means at least one level combination of both the independent variables (Group1 and Group2) has different mean that the others.
- Create bar plot for grouped data of two columns in base R.
- How to create a dot plot using ggplot2 in R?
- How to create horizontal line for a range of values in a plot created by using ggplot2 in R?
- How to create scatterplot by standardizing the columns of a data frame using ggplot2 R?
- How to create transparent bar plot using ggplot2 in R?
- How to create an empty plot using ggplot2 in R?
- How to create a bar plot with ggplot2 using stat_summary in R?
- How to create a boxplot using ggplot2 for single variable without X-axis labels in R?
- How to create stacked plot with density using ggplot2 in R?
- How to create dotted vertical lines in a plot using ggplot2 in R?
- How to create a plot using ggplot2 by including values greater than a certain value in R?
- How to create violin plot for categories with grey color palette using ggplot2 in R?
- How to create a plot title with unicode characters using ggplot2 in R?
- How to create the stacked bar plot using ggplot2 in R with labels on the plot?
- How to create bar plot with log values using ggplot2 in R?