I've used the predict() function to estimate the value of the dependent variable, for two groups, with a few other conditions specified. I want to represent values for these two groups side-by-side in a graph and guess a bar chart is the best way to go:
barplot(brazil$reelected, brazil$corruption,
names.arg = c("Not reelected", "Reelected"),
main = "Corruption level by whether not mayor reelected",
xlab = "Whether not mayor reelected",
xlim = c(0,20),
ylab = "Corruption")
brazil is the dataset name. I would like the Y axis to be the dependent variable (corruption) and the two bars to represent levels of corruption for two groups - one is 'not reelected' (or reelected == 0) and 'reelected' (or reelected == 1). When I run this, I get the error message:
Error in barplot.default(brazil$reelected, brazil$corruption, names.arg = c("Not reelected", :
incorrect number of names
Can you supply a bit of sample data? A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your data. Copy the output and paste it here.
However I did this using the data.table package which is just a bit different than most of the rest of R so my code may [will?] look very weird to you. If I am on the right track, I can translate the code into standard and/or tidyverse code or leave you to explore data.table which has some very interesting features.
I named your original sample data set dat1
# read in data as data.frame called "dat1"
library(ggplot2)
library(data.table)
setDT(dat1) # convert to data.table.
# Create two new binary variables based on "reelected" & "corruption" variables
dat1[ , status := fifelse( reelected == 1, "Yes", "No")]
dat1[ , corrupt := fifelse(corruption == 0, "No", "Yes")]
# This can be done as one step
dat2 <- dat1[, .(status, corrupt)]
dat3 <- dat2[, .N, by = .(status, corrupt)]
## plot
ggplot(dat3, aes(status, N, fill = corrupt)) + geom_bar(stat = "identity", position=position_dodge()) +
ggtitle( "Corruption level by whether or not mayor reelected") + xlab("Reelection Status") +
ylab("Number of Mayors")