为列中的每个变量绘制一个直方图(单独)

时间:2017-07-08 16:31:05

标签: r ggplot2 histogram

我想为列中的每个变量绘制一个直方图(单独)。使用CSV文件(sample.csv)导入数据,看起来像

ip_addr_player_id,  event_name, level, points_earned, stars_earned, moves
118.93.180.241, Puzzle Complete, Botany Lab Puzzle 1, 1000, 2,   2 
118.93.180.241, Puzzle Complete, Botany Lab Puzzle 2, 1000, 2,   2 
118.93.180.241, Puzzle Complete, Botany Lab Puzzle 3, 1000, 2,   2 
203.166.252.219, Puzzle Complete, Botany Lab Puzzle 1, 1000, 2,  2     
54.166.252.324, Puzzle Complete, Botany Lab Puzzle 5, 1000, 2,  2

鉴于每个ip_addr_player_id都是唯一的,我想为ip_addr_payer_idpoints_earnedstarts_earned绘制直方图(针对每个moves)。

x axis: level; y axis: points_earned/stars_earned/moves(one at a time)

我根据我在网上找到的一个例子尝试了这个;

 library(readr)
 dataIn <- read.csv("sample.csv")
 #View(dataIn)
 library(ggplot2)
 plot <- ggplot(dataIn, aes(level, points_earned, fill=points_earned))+ 
              geom_histogram() + facet_wrap(~ip_addr_player_id)
 plot

但是这段代码没有输出。

2 个答案:

答案 0 :(得分:0)

    dataIn = read.table(text="
    ip_addr_player_id,  event_name, level, points_earned, stars_earned, moves
    118.93.180.241, Puzzle Complete, Botany Lab Puzzle 1, 1000, 2,   2 
    118.93.180.241, Puzzle Complete, Botany Lab Puzzle 2, 800, 2,   2 
    118.93.180.241, Puzzle Complete, Botany Lab Puzzle 1, 1000, 2,   2 
    203.166.252.219, Puzzle Complete, Botany Lab Puzzle 1, 1000, 2,  2     
    54.166.252.324, Puzzle Complete, Botany Lab Puzzle 5, 1000, 2,  2
    ",header=T, sep=",")
    dataIn



    # get uniqe players
    players=unique(dataIn$ip_addr_player_id)
    players
    library(data.table)
    #loop over players
    for (i in players) {
      #print (i)

      #select rows for uniq ip_addr_player_id
      index=which(dataIn$ip_addr_player_id ==i)
      #print(index)

      #get dataframe of the coresponding index
      p1=dataIn[index,]

      # get data table
      DT <- data.table(p1)
     #  print(DT)
     # group by level
     dt1= DT[, sum(points_earned), by = level]
      #save the each plot to a file
       png(filename=sprintf("%s.png",i ))
     # set ip as a title for the graph
     barplot(dt1$V1, names.arg=dt1$level, main = i)
     # do the same for other variables for barplot
      dev.off()
    }

Review a partial result online Example

答案 1 :(得分:-1)

你可以使用循环例如

X = your_dataframe

vector_of_levels_you_want = 1:ncol(your_dataframe)

subset_level_1 = your_dataframe[which(your_dataframe[,column_of_your_level] == "Botany Lab Puzzle 1"),]
subset_level_2 = your_dataframe[which(your_dataframe[,column_of_your_level] == "Botany Lab Puzzle 2"),]
subset_level_3 = your_dataframe[which(your_dataframe[,column_of_your_level] == "Botany Lab Puzzle 3"),]

for (col in vector_of_levels_you_want {
    hist(subset_level_1[,col])
    hist(subset_level_2[,col])
    hist(subset_level_3[,col])
}