【问题标题】:Plot by lines of a data frame in R在 R 中按数据框的行绘制
【发布时间】:2018-03-03 13:55:45
【问题描述】:

我有一张这样的桌子

         hashtag  Daily_Freq men women 
          #a          10       6     4  
          #b          15       5    10   
          #c          20       8    12  

我想为每个数据框线(即每个主题标签)绘制男性和女性的频率。 在这种情况下,我想绘制 3 个条形图,每个条形图有两列 - 一个用于男性频率,另一个用于女性频率。 我该怎么做?

【问题讨论】:

    标签: r dataframe bar-chart lines


    【解决方案1】:

    使用来自reshape2melt 的解决方案如下:

    library(ggplot2)
    library(reshape2)
    
    df <- read.table(text = "hashtag  Daily_Freq men women 
                        '#a'          10       6     4  
                        '#b'          15       5    10   
                        '#c'          20       8    12", 
                     header = TRUE)
    
    ds <- melt(df, id.var = c("hashtag", "Daily_Freq"))
    
    p <- ggplot(ds, aes(x=variable, y=value/Daily_Freq)) 
    p <- p + geom_bar(stat='identity', 
                      position='dodge', 
                      aes(fill=hashtag)) 
    p <- p + scale_colour_discrete()
    p <- p + facet_grid(hashtag ~. )
    show(p)
    

    给予,作为输出,

    【讨论】:

      【解决方案2】:

      一种解决方案是使用gatherggplot2 作为:

      #data
      df <- read.table(text = "hashtag  Daily_Freq men women 
      '#a'          10       6     4  
      '#b'          15       5    10   
      '#c'          20       8    12", header = T, stringsAsFactors = F)
      
      library(tidyverse)
      df <- df %>% select(-Daily_Freq) %>%
               gather(key = Gender, value, -hashtag)
      
      library(ggplot2)
      ggplot(df, aes(x=hashtag, y=value, fill=Gender)) +
      geom_bar(stat='identity', position='dodge')
      

      选项 #2

      ggplot(df, aes(x=Gender, y=value)) +
        geom_bar(stat='identity', position='dodge') + facet_grid(~ hashtag)
      

      【讨论】:

      • 但我想要同时绘制 3 个图,每个主题标签一个
      • @Student1000 如果你能画出并分享你期望的图表,那么我可以帮助你。
      • @Student1000 使用facet_grid寻找另一个选项
      【解决方案3】:

      在使用 barplot() 之前创建一个命名矩阵

      使用apply(),您的表格可以转换为 2x3 矩阵,每个性别一行,每个唯一标签值一列。

      然后,将新创建的矩阵提供给barplot() 内的height 参数。

      # load data
      df <-
          read.table(
              text = "hashtag  Daily_Freq men women
                          '#a'          10       6     4
                          '#b'          15       5    10
                          '#c'          20       8    12"
              , header = TRUE
              , stringsAsFactors = FALSE
          )
      
      # we want three barcharts
      # one for unique hashtag
      # and each with two columns
      # one for men and one for women
      gendered.frequencies.by.hashtag <-
          apply( X = df
                     , MARGIN = 1
                     , FUN = function( i )
                         as.numeric(
                             c( i[["women"]], i[["men"]] )
                             )
          )
      # name the rows
      rownames( x = gendered.frequencies.by.hashtag ) <-
          c( "women", "men" )
      
      # name the columns
      colnames( x = gendered.frequencies.by.hashtag ) <-
          unique( df$hashtag )
      
      # create complementary color scheme
      color.scheme <- c( "#18A4D2", "#D24618" )
      
      # plot the matrix
      png(
          filename = "Gendered_Freq_by_HT.png"
          , res = 300
          , units = "px"
          , height = 1600
          , width = 2800
      )
      barplot(
          height = gendered.frequencies.by.hashtag
          , names.arg = colnames( gendered.frequencies.by.hashtag )
          , legend.text = TRUE
          , args.legend = list(
              x = "topleft"
              , bty = "n"
          )
          , col = color.scheme
          , border = NA
          , beside = TRUE
          , las = 1
          , ylim = c( 0, max( gendered.frequencies.by.hashtag ) )
          , main = "Hashtag Frequencies by Gender"
          , ylab = "Frequency"
      )
      # shut down plot device
      dev.off()
      
      # end of script #
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-07
        • 2021-12-27
        • 1970-01-01
        • 2021-02-10
        • 2016-04-04
        • 2018-04-08
        • 2018-08-01
        • 2013-12-29
        相关资源
        最近更新 更多