First of all, I'm still a beginner. I'm trying to interpret and draw a stack bar plot with R. I already took a look at a number of answers but some were not specific to my case and others I simply didn't understand:
- https://stats.stackexchange.com/questions/31597/graphing-a-probability-curve-for-a-logit-model-with-multiple-predictors
- https://stats.stackexchange.com/questions/47020/plotting-logistic-regression-interaction-categorical-in-r
- Plot the results of a multivariate logistic regression model in R
I've got a dataset dvl
that has five columns, Variant, Region, Time, Person and PrecededByPrep. I'd like to make a multivariate comparison of Variant to the other four predictors. Every column can have one of two possible values:
- Variant:
elk
orieder
. - Region =
VL
orNL
. - Time:
time
orno time
- Person:
person
orno person
- PrecededByPrep:
1
or0
Here's the logistic regression
From the answers I gathered that the library ggplot2
might be the best drawing library to go with. I've read its documentation but for the life of me I can't figure out how to plot this: how can I get a comparison of Variant
with the other three factors?
It took me a while, but I made something similar in Photoshop to what I'd like (fictional values!).
Dark gray/light gray: possible values of Variant
y-axis: frequency
x-axis: every column, subdivided into its possible values
I know to make individual bar plots, both stacked and grouped, but basically I do not know how to have stacked, grouped bar plots. ggplot2
can be used, but if it can be done without I'd prefer that.
I think this can be seen as a sample dataset, though I'm not entirely sure. I am a beginner with R and I read about creating a sample set.
t <- data.frame(Variant = sample(c("iedere","elke"),size = 50, replace = TRUE),
Region = sample(c("VL","NL"),size = 50, replace = TRUE),
PrecededByPrep = sample(c("1","0"),size = 50, replace = TRUE),
Person = sample(c("person","no person"),size = 50, replace = TRUE),
Time = sample(c("time","no time"),size = 50, replace = TRUE))
I'd like to have the plot to be aesthetically pleasing as well. What I had in mind:
- Plot colours (i.e. for the bars):
col=c("paleturquoise3", "palegreen3")
- A bold font for the axis labels
font.lab=2
but not for the value labels (e.g. ´regionin bold, but
VLand
NL` not in bold) #404040
as a colour for the font, axis and lines- Labels for the axes: x:
factors
, y:frequency
Here is one possibility which starts with the 'un-tabulated' data frame,
melt
it, plot it withgeom_bar
inggplot2
(which does the counting per group), separate the plot by variable by usingfacet_wrap
.Create toy data:
Reshape data:
Plot:
There are lots of opportunities to customize the plot, such as setting order of factor levels, rotating axis labels, wrapping facet labels on two lines (e.g. for the longer variable name "PrecededByPrep"), or changing spacing between facets.
Customization (following updates in question and comments by OP)
Add counts to each bar (edit following comments from OP).
The basic principles to calculate the y coordinates can be found in this Q&A. Here I use
dplyr
to calculate counts per bar (i.e.label
ingeom_text
) and theiry
coordinates, but this could of course be done inbase
R,plyr
ordata.table
.I'm basically answering a different question. I suppose this can be seen as perversity on my part, but I really dislike barplots of pretty much any sort. They have always seemed to create wasted space because the present informationed numerical values are less useful that an appropriately constructed table. The
vcd
package offers an extended mosaicplot function that seems to me to be more accurately called a "multivariate barplot that any of the ones I have seen so far. It does require that you first construct a contingency table for which thextabs
function seems a perfect fit.That was the 5 way plot and this is the 5-way plot:
Here is my proposition for a solution with function
barplot
of base R :1. calculate the counts
2. draw the barplot without axis names, saving the bar coordinates
3. label the bars
4. add values inside the bars
Here is what I get (with my random data) :