plotting grouped bar charts in R

前端 未结 3 864
忘掉有多难
忘掉有多难 2020-12-08 17:48

i am trying to plot this data in R -

column1  column2  column3
1-2       abc       10
1-2       def       15
1-2       ghi       20
2-3       abc       80
2-         


        
3条回答
  •  没有蜡笔的小新
    2020-12-08 18:19

    I like to use ggplot2 for this kind of task.

    #Make the data reproducible:
    column1 <- c(rep("1-2", 3), rep("2-3", 3), rep("3-4", 3))
    column2 <- gl(3, 1, 9, labels=c("abc", "def", "ghi"))
    column3 <- c(10, 15, 20, 80, 95, 10, 30, 55, 80)
    
    d <- data.frame(column1=column1, column2=column2, column3=column3)
    
    require(ggplot2)
    ggplot(d, aes(x=column1, y=column3, fill=column2)) + geom_bar(position=position_dodge())
    

    The reason I find this intuitive (after a bit of a learning period) is that you clearly stated what you want on the x and y axes, and we simply tell ggplot that (as well as which variable defines the 'fill' color, and which kind of plot - here, geom_bar - to use.

    enter image description here

提交回复
热议问题