TPre
TPre

Reputation: 25

Combine two bar graphs to compare data with ggplot

i have two graphs created in R with ggplot but considering combining them, the energy released and orginal both plotted on the Y. Similiar to this bar layout only created in excel but groups as my x (top).

ggplot(data=biodiesel, aes(x=ï..Group, y=Energy..J.g., fill=Oil)) +
  geom_bar(stat="identity")+
  theme(legend.title=element_blank()) +
  theme(panel.grid.major = element_blank()) +
  theme(panel.grid.minor = element_blank())+
  theme(legend.justification = c(1, 1), legend.position = c(0.25, 1))+
  theme(legend.background = element_blank(),legend.key = element_blank())+
  ylab("Energy Released (J/g)")+
  xlab("Group Number")

ggplot(data=biodiesel, aes(x=ï..Group, y=Original..J.g., fill=Oil)) +
  geom_bar(stat="identity",position="dodge" )+
  theme(legend.title=element_blank()) +
  theme(panel.grid.major = element_blank()) +
  theme(panel.grid.minor = element_blank())+
  theme(legend.justification = c(1, 1), legend.position = c(1, 1))+
  theme(legend.background = element_blank(),legend.key = element_blank())+
  ylab("Original Energy of Oil (J/g)")+
  xlab("Group Number")  

Data:

Group   Oil Energy (J/g)    Original (J/g)
11  Olive   2600    37000
8   Sunflower   2510.4  34040
9   Avocado 1888.2  40600
1   Rice Bran   1549    37000
2   rapeseed    1255.04 33930
7   olive   1004.16 37000
4   Sesame  1003.2  37,000
5   corn    627 34080
3   rapeseed    501.6   33930
6   olive   314 37000
10  Methanol    278.93  22000 

Upvotes: 0

Views: 1699

Answers (2)

CrunchyTopping
CrunchyTopping

Reputation: 814

Be sure to post data in a form that can be read in directly, like:

 library(ggplot2)
dt<-read.table(text="
Group   Oil Energy    Original
11  Olive   2600    37000
8   Sunflower   2510.4  34040
9   Avocado 1888.2  40600
1   Rice_Bran   1549    37000
2   rapeseed    1255.04 33930
7   olive   1004.16 37000
4   Sesame  1003.2  37000
5   corn    627 34080
3   rapeseed    501.6   33930
6   olive   314 37000
10  Methanol    278.93  22000 ",header=T)

You might need to fiddle with position and width and the legend justification

ggplot(data=dt, aes(fill=Oil)) + 
  geom_bar(mapping = aes(x = Group, y = Original), position=position_nudge(x = 0.2), width=0.2,stat = "identity",color="green" ) +
  geom_bar(mapping = aes(x = Group, y = Energy*5), position=position_nudge(x = -0.2), width=.2, stat = "identity", color = "blue") + 
  scale_y_continuous(name = "Original Energy of Oil (J/g)", 
                     sec.axis = sec_axis(~./5, name = "Energy Released (J/g)"))+ 
  theme(
    axis.title.y = element_text(color = "grey"),
    axis.title.y.right = element_text(color = "blue"))+
  theme(legend.justification = c(3, 1), legend.position = c(0.25, 1))+
  theme(legend.background = element_blank(),legend.key = element_blank())+
  xlab("Group Number") 

enter image description here

This is similar to Plot with 2 y axes, one y axis on the left, and another y axis on the right

Upvotes: 1

ricoderks
ricoderks

Reputation: 1619

To get the same plot as the first plot you show you need to make the table biodiesel wide with gather. With position = "dodge2" you can, but the bars next to each other instead of on top of each other. The only thing is that rapeseed and olive are twice in your data so this will look a bit weird!

biodiesel %>% 
  # make wide
  gather(Energy, value, -Group, -Oil) %>% 
  ggplot(aes(x = Oil,
             y = value,
             fill = Energy)) +
  geom_bar(stat = "identity",
           position = "dodge2")

Upvotes: 1

Related Questions