1
votes

I have some data, and I want to plot confidence interval using ggplot's stat_summary.

My code looks like this:

file <- read.csv(filepath)
ggplot(file, aes(shop, income, colour = season)) + stat_summary(size = 0.8)

enter image description here

But I want to get something like this:

enter image description here

So my questions are:

  1. How can I change names of "shop" columns?
  2. How can I change length of summary lines?
  3. How can I set distance between two datalines in one column?
1

1 Answers

1
votes

Try this approach. The size of the bars depends on how intervals are being computed. For the other points you can use position_dodge() and scale_x_discrete(). Here the code:

library(ggplot2)  
#Code
file <- read.csv('sales.csv')
#Plot
ggplot(file, aes(shop, income, colour = season)) +
  stat_summary(size = 0.8,position = position_dodge(0.25))+
  scale_x_discrete(limits=c("Shop â„–1","Shop â„–2"),
                   labels=c('Shop1','Shop2'))

Output:

enter image description here

For y-axis, try this:

#Plot 2
ggplot(file, aes(shop, income, colour = season)) +
  stat_summary(size = 0.8,position = position_dodge(0.25))+
  scale_x_discrete(limits=c("Shop â„–1","Shop â„–2"),
                   labels=c('Shop1','Shop2'))+
  scale_y_continuous(breaks = c(1050,1100,1150))

Output:

enter image description here