Tags » Barplot

Barplots across variables in R

Here’s a good example of how useful sapply can be. I have some data from Qualtrics, and each response is coded in its own variable. 262 more words

Didier Ruedin

Basic ggplot - Barplot

Make a data frame with a categorial variable as factor or character and plot a barplot using geom_bar()

N=1000
a=c("freshman","graduate","junior","senior","sophmore")
students=factor(sample(1:5,N,replace=TRUE),labels=a)

plot(table(students))
barplot(table(students))
mydata=data.frame(as.character(students))

p=ggplot(mydata, aes(x = students))
p=p+geom_bar(fill=I(rgb(100,100,250,maxColorValue=255)))
p

# Also works with character variables

mydata=data.frame(as.character(students))
p=ggplot(mydata, aes(x = students))
p=p+geom_bar(fill=I(rgb(100,100,250,maxColorValue=255)))
p=p+scale_x_discrete("")
p=p+scale_y_continuous("")
p


# Columns side-by-side
library(reshape2)
df = melt(data.frame(A=c(3, 10), B=c(5, 20), 
                 experiment=c("A", "A & B")),
      variable.name="metric")

ggplot(df, aes(experiment, value, fill=metric)) + 
  geom_bar(position="dodge",stat="identity")

# Barplot for timeseries ... 47 more words