Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bar plot without grouping variable

Tags:

plot

r

bar-chart

This seems like to simplest thing to do, but I have not been able to figure this out on R. For descriptive purposes, I want to create one bar graph that show the means and error plots of multiple questions/variables. My data is based on anonymous responses so there is no grouping variables.

Is there a way to do this on R? Below is an example of what my data looks like. I would like to plot mean and standard deviation of each variable next to each other in the same bar graph.

dat <- data.frame(satisfaction = c(1, 2, 3, 4),
                  engaged = c(2, 3, 4, 2),
                  relevant = c(4, 1, 3, 2),
                  recommend = c(4, 1, 3, 3))
like image 281
Tawk_Tomahawk Avatar asked May 16 '16 22:05

Tawk_Tomahawk


People also ask

Can bar graphs be used with multiple variables?

Multiple variable bar charts can be created from a single data file or from different data files. Typically, the X values (for vertical bars) are the same for each of the bar charts. However, the X values may be different between bar charts. When creating stacked bar charts, each row of data is considered a stack.

What is a grouped bar plot?

A grouped bar chart (aka clustered bar chart, multi-series bar chart) extends the bar chart, plotting numeric values for levels of two categorical variables instead of one. Bars are grouped by position for levels of one categorical variable, with color indicating the secondary category level within each group.

How do I create a bar graph in Excel with 3 variables?

Step 1: Open the Excel sheet and enter the values of 3 variables and save the variables with names. Step 2: Select everything, including headers, and open the insert tab in the navigation menu. Step 3: Navigate to the charts session and click on bar graphs.

What is a segmented bar chart?

A segmented bar graph is a graphical representation of data in which the bars are divided into segments. The segments can be different colors, sizes, or shapes to make the data easier to understand. Segmented bar graphs are often used to compare data between other groups.


2 Answers

What you could do is reshape the data into long format with reshape2 (or data.table or tidyr) without specifying an id-variable and using all columns as measure variables. After that you can create a plot with for example ggplot2. Using:

library(reshape2)
library(ggplot2)

# reshape into long format
dat2 <- melt(dat, measure.vars = 1:4)  # or just: melt(dat)

# create the plot
ggplot(dat2, aes(x = variable, y = value)) +
  stat_summary(geom = 'bar', fun.y = 'mean', width = 0.7, fill = 'grey') +
  stat_summary(geom = 'errorbar', width = 0.2, size = 1.5) +
  theme_minimal(base_size = 14) +
  theme(axis.title = element_blank())

gives:

enter image description here


Update: As @GavinSimpson pointed out in his answer: for visualizing means and standard errors, a barplot is not the best alternative. As an alternative you could also use geom_pointrange:

ggplot(dat2, aes(x = variable, y = value)) +
  stat_summary(geom = 'pointrange', fatten = 5, size = 1.2) +
  theme_minimal(base_size = 14) +
  theme(axis.title = element_blank())

which gives:

enter image description here

like image 174
Jaap Avatar answered Oct 08 '22 06:10

Jaap


Whilst I know you asked for a barplot, a dotplot of the data is an alternative visualisation that focuses on the means and standard errors. If the drawing of a bar all the way to 0 is not that informative, the dotplot is a good alternative.

Reusing the objects and code from @Procrastinatus Maximus' answer we have:

ggplot(dat2, aes(x = variable, y = value)) +
  stat_summary(geom = 'point', fun.y = 'mean', size = 2) +
  stat_summary(geom = 'errorbar', width = 0.2) +
  xlab(NULL) + 
  theme_bw()

which produces

enter image description here

like image 28
Gavin Simpson Avatar answered Oct 08 '22 08:10

Gavin Simpson