1

I'm little bit stuck on ggplot2 trying to plot several data frame in one plot.

I have several data frame here I'll present just two exemples.

The data frame have the same Header but are different. Let say that I want to count balls that I have in 2 boxes.

name=c('red','blue','green','purple','white','black')
value1=c(2,3,4,2,6,8)
value2=c(1,5,7,3,4,2)

test1=data.frame("Color"=name,"Count"=value1)
test2=data.frame("Color"=name,"Count"=value2)

What I'm trying to do it's to make a bar plot of my count. At the moment what I did it's :

(plot_test=ggplot(NULL, aes(x= Color, y=Count)) + 
    geom_bar(data=test1,stat = "identity",color='green')+
    geom_bar(data=test2,stat = "identity",color='blue')
)

Problem is that my plots are overlapping themselves

I want to have x=Color and y=Count, and barplot of test2 data frame next to test1. Here there are overlapping themselves. So I'll have same name twice in x but I want to plot the data frames in several color and got in legend the name.

For example "Green bar" = test1 "Blue bar" = test2

Thank you for your time and your help.

Best regards

3 Answers 3

4

You have two options here:

Either tweak the size and position of the bars

ggplot(NULL, aes(x= Color, y=Count)) + 
geom_bar(data=test1, aes(color='test1'), stat = "identity",
         width=.4, position=position_nudge(x = -0.2)) +
geom_bar(data=test2, aes(color='test2'), stat = "identity", 
         width=.4, position=position_nudge(x = 0.2))

enter image description here or what I recommend is join the two data frames together and then plot

library(dplyr)
test1 %>% 
  full_join(test2, by = 'Color') %>% 
  data.table::melt(id.vars = 'Color') %>% 
  ggplot(aes(x= Color, y=value, fill = variable)) + 
  geom_bar(stat = "identity", position = 'dodge')

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

In your first solution, OP's "legend condition" is not satisfied.
First one look very easy to understand and It's what I want but legend is missing. I'm going to try the second one. Thank you for your help
the legend should be there now :)
Thanks I didn't see that you edit. Look perfect but I'll try the second one and try to understand how it works Thank you
Both are very good thanks a lot But for both how I can modify the legend? I don't find... Thanks :)
|
3

Try this:

name=c('red','blue','green','purple','white','black')
value1=c(2,3,4,2,6,8)
value2=c(1,5,7,3,4,2)

test1=data.frame("Color"=name,"Count"=value1)
test2=data.frame("Color"=name,"Count"=value2)

test1$var <- 'test1'
test2$var <- 'test2'

test_all <- rbind(test1,test2)


(plot_test=ggplot(data=test_all) + 
  geom_bar(aes(x=Color,y=Count,color=var),
           stat = "identity", position=position_dodge(1))+
  scale_color_manual(values = c('green', 'blue'))
)

1 Comment

Look very good and it's exactly what I was looking for. Thank you
0

This will do what you were trying to do:

balls <- data.frame(
  count = c(c(2,3,4,2,6,8),c(1,5,7,3,4,2)),
  colour = c(c('red','blue','green','purple','white','black'),c('red','blue','green','purple','white','black')),
  box = c(rep("1", times = 6), rep("2", times = 6))
)

ggplot(balls, aes(x = colour, y = count, fill = box)) +
  geom_col() +
  scale_fill_manual(values = c("green","blue"))

This is better because it facilitates comparisons between the box counts:

ggplot(balls, aes(x = colour, y = count)) +
  geom_col() +
  facet_wrap(~ box, ncol = 1, labeller = as_labeller(c("1" = "Box #1", "2" = "Box #2")))

1 Comment

Thank, look greats but it's not exactly what I want. Response before do the job. In my example it can work but the things is that in reality I have like 11 data frames and don't know there length. Maybe I can change in "c(rep("1",times=length(test1),rep"2",times=length(test2)" But I keep your code because it can be usefull for another plot that I want to do so thank a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.