为列中的每个变量绘制一个直方图(单独) [英] Plot one histogram(separate) for each variable in the column

查看:43
本文介绍了为列中的每个变量绘制一个直方图(单独)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  ip_addr_player_id,事件名称,级别,获得的积分,获得的星星,移动118.93.180.241,拼图完成,植物学实验室拼图1,1000,2,2118.93.180.241,拼图完成,植物学实验室拼图2、1000、2、2118.93.180.241,拼图完成,植物学实验室拼图3,1000,2,2203.166.252.219,拼图完成,植物学实验室拼图1、1000、2、254.166.252.324,拼图完成,植物学实验室拼图5,1000,2,2 

鉴于每个 ip_addr_player_id 是唯一的,我想为 points_earned starts_earned ip_addr_payer_id )>和 moves .

我根据可以在网上找到的示例进行了尝试;

 库(阅读器)dataIn<-read.csv("sample.csv")#View(dataIn)库(ggplot2)绘图<-ggplot(dataIn,aes(level,points_earned,fill = points_earned))+geom_histogram()+ facet_wrap(〜ip_addr_player_id)阴谋 

但是此代码没有输出.

解决方案

  dataIn = read.table(text =ip_addr_player_id,event_name,等级,points_earned,stars_earned,移动118.93.180.241,拼图完成,植物学实验室拼图1,1000,2,2118.93.180.241,拼图完成,植物学实验室拼图2,800,2,2118.93.180.241,拼图完成,植物学实验室拼图1,1000,2,2203.166.252.219,拼图完成,植物学实验室拼图1、1000、2、254.166.252.324,拼图完成,植物学实验室拼图5,1000,2,2,header = T,sep =",)数据输入#获得uniqe玩家players = unique(dataIn $ ip_addr_player_id)玩家们库(data.table)#循环播放玩家对于(我在玩家中){#打印(i)#选择uniq ip_addr_player_id的行索引=其中(dataIn $ ip_addr_player_id == i)#print(索引)#获取核心索引的数据框p1 = dataIn [index,]#获取数据表DT<-data.table(p1)#打印(DT)#按级别分组dt1 = DT [,sum(points_earned),按=级别]#将每个图保存到文件中png(filename = sprintf(%s.png",i))#将ip设置为图形的标题barplot(dt1 $ V1,names.arg = dt1 $ level,main = i)#对barplot的其他变量执行相同的操作dev.off()} 

I want to plot one histogram(separate) for each variable in the column. The data is import using a CSV file(sample.csv) and looks like

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

Given each ip_addr_player_id is unique, I want to plot histograms (for each ip_addr_payer_id) for points_earned, starts_earned and moves.

I tried this based on an example I could find online;

 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

But this code gives me no output.

解决方案

    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

这篇关于为列中的每个变量绘制一个直方图(单独)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆