R Plot显示有多少用户%发布过帐%

时间:2018-03-28 14:46:30

标签: r plot percentage

大家好,我有一个关于绘图的问题。

我需要一个线图,显示有多少用户写了哪些%贴子。
例如:25%的用户写了80%的帖子

graph

输出输出: data

data
我从csv将数据读入R并附上标题 现在,当我尝试绘制它时:

    plot(UserPc,PostingsPc,ylab = "Users", xlab= "Postings",type="l")
这个情节只是一个黑色的方块,停了下来

1 个答案:

答案 0 :(得分:0)

UserPcPostingsPc组合","和"%"所以read.csv将它们解释为字符串(它读作因子)而不是数字。如果您运行str(myData),您会看到此信息。如果你想绘制它们,你需要将它们转换成数字,查看你的数据需要替换","用"。"并删除"%"。 gsub对此有用,并且使整个操作成为自己的功能很方便。像这样:

MyData <- read.csv(file="data.csv", header=TRUE, sep=";",stringsAsFactors = FALSE)
#write a function that removes all "%" from a string converts "," to "." and returns a numeric
#divide by 100 because it's a percentage
convert <- function(stringpct){
  as.numeric(gsub("%","",gsub(",",".",stringpct)))/100
}
MyData$UserPc <- convert(MyData$UserPc)
MyData$PostingsPc <- convert(MyData$PostingsPc)
attach(MyData)
plot(UserPc,PostingsPc,ylab = "Users", xlab= "Postings",type="l")