简体   繁体   English

在R中按行绘制数据帧

[英]Plot by lines of a data frame in R

I have a table looking like this 我有一张像这样的桌子

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

I want to plot for each data frame line, ie for each hashtag, the frequency of men and women. 我想为每个数据框线,即每个主题标签,绘制男性和女性的频率。 In this case I would want to plot 3 barplots, each one with two columns - one for men and anoher for women frequencies. 在这种情况下,我想绘制3个条形图,每个条形图有两列-一个用于男性,另一个用于女性频率。 How can I do this? 我怎样才能做到这一点?

A solution that makes use of melt from reshape2 is the below: 以下是利用来自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)

giving, as output, 作为输出给出

在此处输入图片说明

One solution is using gather and ggplot2 as: 一种解决方案是使用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')

在此处输入图片说明

Option #2 选项#2

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

在此处输入图片说明

Create a Named Matrix Prior to Using barplot() 在使用barplot()之前创建一个命名矩阵

Using apply() , your table can be transformed into a 2x3 matrix, one row per gender and one columns per unique hashtag value. 使用apply() ,您的表可以转换为2x3矩阵,每个性别一行,每个唯一的hashtag值一行。

Afterwards, supply the newly created matrix into the height argument within barplot() . 然后,将新创建的矩阵提供给barplot()height参数。

巴哈特的PNG

# 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 #

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM