- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- 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 join the points of a point chart if X-axis values are categorical in R using ggplot2?
A point chart is usually drawn to see the relationship between two continuous variables and it is also called scatterplot but if the independent variable is categorical then we simply call it a point chart. Often, we want to join or connect the points of a point chart to visually represent the variation of categories of the independent variable and make it a line chart. This can be done by setting stat_summary argument geom to line and setting group = 1 in aes.
Example
Consider the below data frame −
Class<-c("A","B","C") Frequency<-c(23,24,12) df<-data.frame(Class,Frequency) df
Output
Class Frequency 1 A 23 2 B 24 3 C 12 library(ggplot2)
Creating a simple point chart −
ggplot(df,aes(x,y,group=1))+geom_point()
Output
Creating the plot with connected points −
ggplot(df,aes(x,y,group=1))+geom_point()+stat_summary(fun.y=sum,geom="line")
Output
Advertisements