[R] Two geom_bar with counts to put in the same plot

Francesca |r@nce@c@@p@ncotto @end|ng |rom gm@||@com
Tue Dec 3 10:18:01 CET 2019


Dear Contributors,
I would like to ask help on how to create a  plot that is the overlapping
of two other plots.
It is a geom_bar structure, where I want to count the occurrences of two
variables, participation1 and participation2 that I recoded as factors as
ParticipationNOPUN and ParticipationPUN to have nice names in the legend.
The variables to "count" in the two plots are delta11_L and delta2_L
These are my data and code to create the two plots. I would like to put
them in the same plot as superimposed areas so that I see the change in the
distribution of counts in the two cases.
This is DB:

participation1 participation2 ParticipantsNOPUN ParticipantsPUN delta11_L
delta2_L
  [1,]              1              1                 2               2
    0        0
  [2,]              1              1                 2               2
  -10      -10
  [3,]              1              1                 2               2
  -10        0
  [4,]              1              1                 2               2
    0        0
  [5,]              1              1                 2               2
    0        0
  [6,]              1              1                 2               2
    0        0
  [7,]              1              0                 2               1
  -30       30
  [8,]              1              1                 2               2
    0       10
  [9,]              1              0                 2               1
   10       40
 [10,]              1              1                 2               2
    0        0
 [11,]              0              0                 1               1
   20        0
 [12,]              1              1                 2               2
   10        0
 [13,]              1              1                 2               2
    0        0
 [14,]              1              1                 2               2
    0        0
 [15,]              1              1                 2               2
   20       10
 [16,]              1              1                 2               2
    0        0
 [17,]              1              1                 2               2
    0        0
 [18,]              1              1                 2               2
  -10       30
 [19,]              0              0                 1               1
   30       10
 [20,]              1              1                 2               2
   10       10
 [21,]              1              1                 2               2
    0        0
 [22,]              1              1                 2               2
    0        0
 [23,]              1              1                 2               2
    0      -10
 [24,]              1              1                 2               2
    0      -20
 [25,]              1              1                 2               2
   10      -10
 [26,]              1              1                 2               2
    0        0
 [27,]              1              1                 2               2
    0        0

First PLOT(I need to subset the data to eliminate some NA. NB: the two
dataframes end up not having the same number of rows for this reason):

ggplot(data=subset(DB, !is.na(participation1)), aes(x = delta11_L, fill
=ParticipantsNOPUN))+
         geom_bar(position = "dodge")+ theme_bw(base_size = 12) +
labs(x="Delta Contributions (PGG w/out punishment)")+
  theme(legend.position = "top",legend.title = element_blank())
+scale_fill_brewer(palette="Set1")

Second PLOT:

 ggplot(DB, aes(x = delta2_L, fill =ParticipantsPUN)  , aes(x = delta2_L,
fill =ParticipantsPUN))+
  geom_bar(position = "dodge")+ theme_bw(base_size = 12) + labs(x="Delta
Contributions (PGG w/punishment)")+
  theme(legend.position = "top",legend.title = element_blank())
+scale_fill_brewer(palette="Set1")



is it possible to create a density plot of the two counts data on the same
plot?
Do I need to create a variable count or long data format?
Thanks

	[[alternative HTML version deleted]]



More information about the R-help mailing list