- Trending Categories
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 create bar plot with positive and negative values in R?
To create bar plot with positive and negative values, we can make use of ggplot function.
For example, if we have a data frame called df that contains a categorical column say C and a numerical column Num which has some positive and some negative values then the bar plot for this data can be created by using the below command −
ggplot(df,aes(C,Num))+geom_bar(stat="identity")
Example
Following snippet creates a sample data frame −
Category<-c("Egypt","Sudan","Turkey","Indonesia") Response_Score<-c(10,-2,4,-5) df<-data.frame(Category,Response_Score) df
Output
The following dataframe is created −
Category Response_Score 1 Egypt 10 2 Sudan -2 3 Turkey 4 4 Indonesia -5
To load ggplot2 package and create bar chart for data stored in df, add the following code to the above snippet −
library(ggplot2) ggplot(df,aes(Category,Response_Score))+geom_bar(stat="identity")
Output
If you execute all the above given snippets as a single program, it generates the following output −
- Related Articles
- How to create plotly bar chart with negative values in R?
- How to create bar plot with log values using ggplot2 in R?
- How to create a bar plot with bars for missing values in R?
- How to create a bar plot with ggplot2 using stat_summary in R?
- How to create empty bar plot in base R?
- How to convert negative values in an R data frame to positive values?
- How to create a bar plot using ggplot2 with one bar having black border in R?
- How to create bar plot with gradient colors using ggplot2 in R?\n
- How to create transparent bar plot using ggplot2 in R?
- How to create the stacked bar plot using ggplot2 in R with labels on the plot?
- How to create a stacked bar plot with vertical bars in R using ggplot2?
- How to create bar plot in base R with different limits for Y-axis?
- How to create a bar plot in R filled with color palette in RColorBrewer package?
- How to create a bar plot using ggplot2 with percentage on Y-axis in R?
- How to create horizontal lines for each bar in a bar plot of base R?

Advertisements