Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot bar plot side by side using two variables [duplicate]

Tags:

r

I want to create a barplot using ggplot in R studio using two variables side by side. I tried following other people suggestions I found online, but I cant get it to work.

Here's the data I'm using:

x <- c(5,17,31,9,17,10,30,28,16,29,14,34)

y <- c(1,2,3,4,5,6,7,8,9,10,11,12)

day <- c(1,2,3,4,5,6,7,8,9,10,11,12)

So, what I'm trying to do is have days on the x-axis and side by side barplots of x and y (with x & y being colored) corresponding to the day number.

First thing i did was make a data frame :

df1 <- data.frame(x,y,day)

and then I tried:

ggplot(df1, aes(x = day, y = x,y)) + geom_bar(stat = "identity",color = x, width = 1, position="dodge")

But I just can't get it to work properly. Any suggestions as to how I'd achieve this?

like image 403
Electrino Avatar asked Mar 15 '17 21:03

Electrino


People also ask

What does Geom_bar do in R?

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.

What is a side by side bar graph?

As a variation of a bar chart, a side-by-side bar chart is similar to a stacked bar chart, except it puts bars side by side instead of stacking them. Both side-by-side bar and stacked bar charts add a second dimension based on a standard bar chart. But stacked bar charts tend to show the part-to-whole relation.

How do I make a side by side graph in ggplot2?

Side By Side Bar Graphs To obtain side by side bar graphs in ggplot2, we need a lot of parts on top of the ggplot() command. geom_bar(stat = “identity”, position = position_dodge(), alpha = 0.75) gives the side by side bar graphs. ylim(0, 800) gives limits on the y-axis values. The geom_text() line adds labels to the bar graphs.

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')

How to create two ggplots in R?

Now, we can create two ggplots with the following R code: The data object ggp1 contains a density plot and the data object ggp2 contains a scatterplot. Note that we could store any type of graphic or plot in these data objects.

How to make the bars UN-stack in ggplot?

Next we use position = "dodge"within geom_col()to make the bars un-stack. By default they will be stacking due to the format of our data and when he used fill = Statwe told ggplot we want to group the data on that variable. ggplot(dat_long, aes(x = Batter, y = Value, fill = Stat)) + geom_col(position = "dodge")


2 Answers

You have the right idea, I think the melt() function from the reshape2 package is what you're looking for.

library(ggplot2)
library(reshape2)

x <- c(5,17,31,9,17,10,30,28,16,29,14,34)
y <- c(1,2,3,4,5,6,7,8,9,10,11,12)
day <- c(1,2,3,4,5,6,7,8,9,10,11,12)


df1 <- data.frame(x, y, day)
df2 <- melt(df1, id.vars='day')
head(df2)

ggplot(df2, aes(x=day, y=value, fill=variable)) +
    geom_bar(stat='identity', position='dodge')

enter image description here

EDIT I think the pivot_longer() function from the tidyverse tidyr package might now be the better way to handle these types of data manipulations. It gives quite a bit more control than melt() and there's also a pivot_wider() function as well to do the opposite.

library(ggplot2)
library(tidyr)

x <- c(5,17,31,9,17,10,30,28,16,29,14,34)
y <- c(1,2,3,4,5,6,7,8,9,10,11,12)
day <- c(1,2,3,4,5,6,7,8,9,10,11,12)


df1 <- data.frame(x, y, day)
df2 <- tidyr::pivot_longer(df1, cols=c('x', 'y'), names_to='variable', 
values_to="value")
head(df2)

ggplot(df2, aes(x=day, y=value, fill=variable)) +
    geom_bar(stat='identity', position='dodge')
like image 71
TaylorV Avatar answered Oct 08 '22 17:10

TaylorV


Or you could use facet_wrap to produce two plots:

  library("ggplot2")
  library("reshape")
  x <- c(5,17,31,9,17,10,30,28,16,29,14,34)
  y <- c(1,2,3,4,5,6,7,8,9,10,11,12)
  day <- c(1,2,3,4,5,6,7,8,9,10,11,12)
  df1 <- data.frame(x,y,day)
  df2 <- reshape::melt(df1, id = c("day"))
  ggplot(data = df2, aes(x = day, y = value, fill = variable)) + geom_bar(stat = "identity")+ facet_wrap(~ variable) + scale_x_continuous(breaks=seq(1,12,2))

enter image description here If you want the bars with color according to the day use fill = day:

ggplot(data = df2, aes(x = day, y = value, fill = day)) + geom_bar(stat = "identity") + facet_wrap(~ variable) + scale_x_continuous(breaks=seq(1,12,2)) 

enter image description here

like image 26
Edgar Santos Avatar answered Oct 08 '22 15:10

Edgar Santos