Search code examples
rlinelinechartstandard-deviation

Line Plot with standard deviation


I want to create a line chart, with two lines that shows the standard deviation for each for each line. At the moment, I have a line chart, that shows the two lines. My Code is this, Categories is the name for the x-axes, Result 1/2 are the Results and SD 1/2 is the standard deviation.

categories <-c("Traditionsbewusst / Heimatverbunden","Bekanntheit
+ ","Jugendlich / Modern
+ ","Professionell
+ ","Sozial engagiert
+ ","Aufstrebend / Motiviert
+ ","Umwelt / Nachhaltigkeit
+ ","Sympathisch
+ ","Familienfreundlich
+ ","Mitreißend
+ ","Posetives Image
+ ","Teamgeist
+ ","Inovativ
+ ")


Result1<-c(2.34,1.76,2.66,2.85,2.45,2.66,2.64,2.89,2.61,2.80,2.94,2.72,2.82)
Result2<-c(2.08,1.29,2.41,2.39,2.11,2.08,2.34,2.25,2.19,2.24,2.58,2.19,2.42)

SD1<-c(0.89,0.93,0.85,0.92,0.78,0.86,0.86,1.01,0.83,0.86,0.92,0.90,0.97)
SD2<-c(0.96,0.71,0.80,0.85,0.89,1.00,0.76,0.94,0.87,0.93,0.94,0.95,0.85)

par(mar = c(15, 3, 3, 3))
plot(Result1,type = "b",main = "Profil Image",xlab = "",ylab = "Bewertung",axes =FALSE,ylim = c(1,4))
axis(1,at=1:13,labels = categories,las=2,cex.axis=0.8)
lines(Result2,type = "b")
axis(2)

Solution

  • You can use arrows, see this post too. If you put them on the same graph, it looks really bad:

    par(mar = c(15, 3, 3, 3))
    plot(Result1,type = "b",main = "Profil Image",xlab = "",ylab = "Bewertung",axes =FALSE,ylim = c(0,4))
    arrows(x0=1:length(SD1), y0=Result1-SD1, x1=1:length(SD1),code=3,y1=Result1+SD1,angle=90, length=0.05)
    arrows(x0=1:length(SD2), y0=Result2-SD2, x1=1:length(SD2),code=3,y1=Result2+SD2,angle=90, length=0.05)
    axis(1,at=1:13,labels = categories,las=2,cex.axis=0.8)
    lines(Result2,type = "b")
    axis(2)
    

    enter image description here

    Maybe put them side by side:

    par(mfrow=c(1,2))
    par(mar = c(15, 3, 3, 3))
    plot(Result1,type = "b",main = "Profil Image",xlab = "",ylab = "Bewertung",axes =FALSE,ylim = c(0,4))
    arrows(x0=1:length(SD1), y0=Result1-SD1, x1=1:length(SD1),code=3,y1=Result1+SD1,angle=90, length=0.05)
    axis(1,at=1:13,labels = categories,las=2,cex.axis=0.8)
    axis(2)
    
    plot(Result2,type = "b",main = "Profil Image",xlab = "",ylab = "Bewertung",axes =FALSE,ylim = c(0,4))
    arrows(x0=1:length(SD2), y0=Result2-SD2, x1=1:length(SD2),code=3,y1=Result2+SD2,angle=90, length=0.05)
    axis(1,at=1:13,labels = categories,las=2,cex.axis=0.8)
    axis(2)
    

    enter image description here