R 图显示有多少 User% 做了 Postings%
R Plot that shows how many User% did Postings%
大家好,我有一个关于在 R 中绘图的问题。
我需要一个线图来显示有多少 %users 写了哪些 %postings。
例如:25% 的用户写了 80% 的帖子
Dput输出:
data
我从 csv 中将数据读入 R 并将其附加到 headers.
现在,当我尝试用以下方式绘制它时:
plot(UserPc,PostingsPc,ylab = "Users", xlab= "Postings",type="l")
情节只是一个黑色方块,halp
UserPc
和 PostingsPc
包含“,”和“%”,因此 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")
大家好,我有一个关于在 R 中绘图的问题。
我需要一个线图来显示有多少 %users 写了哪些 %postings。
例如:25% 的用户写了 80% 的帖子
Dput输出:
data
我从 csv 中将数据读入 R 并将其附加到 headers.
现在,当我尝试用以下方式绘制它时:
plot(UserPc,PostingsPc,ylab = "Users", xlab= "Postings",type="l")
情节只是一个黑色方块,halp
UserPc
和 PostingsPc
包含“,”和“%”,因此 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")