Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlay two geom_bar like two barplots with par(new=TRUE)

I currently have a plot similar to this:

sourceTable<-"dateValues;total;inHospital
2014-01-01; 80; 20
2014-01-02; 90; 15
2014-01-03; 98; 16
2014-01-04; 98; 17
2014-01-05; 100; 28
2014-01-06; 110; 28
2014-01-07; 122; 30
2014-01-08; 110; 25
2014-01-09; 100; 22
2014-01-00; 90; 20
2014-01-11; 90; 15
2014-01-12; 80; 13
2014-01-13; 82; 10"

timetable<-read.table(textConnection(sourceTable), sep=";", head=T, dec=",")
barplot(timetable$total, border="red", axes=FALSE, ylim=c(0, max(timetable$total)))
par(new=TRUE)
barplot(timetable$inHospital, border="blue", axes=FALSE, ylim=c(0, max(timetable$total)))

which gives me this plot:

two overlayed barplots

Is there a way to do this with ggplot and geom_bar like this:

library(ggplot2)
ggplot(timetable, aes(x=dateValues, y=total))
  +geom_bar(stat="identity", fill="red", colour="red")

How can I overlay a second ggplot over the first one, i.e.:

ggplot(timetable, aes(x=dateValues, y=inHospital))
  +geom_bar(stat="identity", fill="red", colour="blue")
like image 780
Johann Horvat Avatar asked Sep 04 '14 09:09

Johann Horvat


People also ask

What does the bar represent in multiple bar plots?

In multiple bar plots, we have various bar plots in the form of horizontal and vertical rectangular bars. In these multiple bar plots, the bar represents the relationship between numeric and categorical variables.

What is the difference between Geom_Col and bar chart?

Bar charts. There are two types of bar charts: geom_bar() and geom_col(). geom_bar() makes the height of the bar proportional to the number of cases in each group (or if the weight aesthetic is supplied, the sum of the weights). If you want the heights of the bars to represent values in the data, use geom_col() instead.

How to overlap two barplots in Seaborn?

It is built on the top of matplotlib library and also closely integrated to the data structures from pandas. Bar Plot is used to represent categories of data using rectangular bars. We can overlap two barplots in seaborn by creating subplots.

How to create a barplot with multiple variables in R?

The following code shows how to create the barplot with multiple variables using the geom_bar () function to create the bars and the ‘dodge’ argument to specify that the bars within each group should “dodge” each other and be displayed side by side. ggplot (df, aes(fill=food, y=sales, x=stadium)) + geom_bar (position='dodge', stat='identity')


1 Answers

Try this:

library(ggplot2)

ggplot(data = timetable, aes(x = dateValues, y = total)) + 
  geom_bar(stat = "identity", fill = "grey", colour = "red")+
  geom_bar(data = timetable, aes(x = dateValues, y = inHospital),
           stat = "identity", fill = "grey", colour = "blue")

enter image description here


EDIT: Alternative - the right way of doing it - transform the data before plotting:

library(reshape2)
library(ggplot2)

# transform the data - melt
timetable$outHospital <- timetable$total - timetable$inHospital
df <- melt(timetable, id = c("dateValues", "total"))

# plot in one go
ggplot(data = df, aes(x = dateValues, y = value, fill = variable)) + 
  geom_bar(stat = "identity")

enter image description here

like image 131
zx8754 Avatar answered Nov 16 '22 02:11

zx8754