[英]How to read one column value from CSV file in R in distinct format
How to use legend in R for the following data 如何在R中将图例用于以下数据
ID Gender Age Site Times
1 Male 24 Facebook 10
2 Female 24 Linkedin 10
3 Male 24 Twitter 10
4 Female 24 Myspace 10
5 Male 24 Facebook 10
6 Female 24 linkedin 10
7 Male 24 Facebook 10
TO Read the CSV I've used 读取我使用过的CSV
pd.readcsv <- read.csv(file = "snsite.csv")
To design the Pie chart I've used 设计我使用的饼图
pie(pd.freq[order(pd.freq,decreasing = T)],
col = c("Blue","Green","Yellow","Brown","Green"),
border = NA, main = "Site Usage")
Now to show legend at top right or top left, how to read one column(Site) value in distinct form from csv file and store it in a variable and used it in legend function? 现在要在右上角或左上角显示图例,如何从csv文件中以不同的形式读取一列(Site)值并将其存储在变量中并在图例函数中使用?
I wasn't sure where you were getting the pd.freq object from, but assuming you are trying to get a pie chart showing per site how many times each site was visited, then this should do the trick. 我不确定从哪里获得pd.freq对象,但是假设您试图获取一个饼图,该饼图显示每个站点访问了每个站点的次数,那么就可以解决问题。
# Read dataframe, I would recommend including the stringsAsFactors = FALSE argument to your read.csv command
pd.readcsv <- read.table(text = "ID Gender Age Site Times
1 Male 24 Facebook 10
2 Female 24 Linkedin 10
3 Male 24 Twitter 10
4 Female 24 Myspace 10
5 Male 24 Facebook 10
6 Female 24 linkedin 10
7 Male 24 Facebook 10",
header = TRUE, stringsAsFactors = FALSE)
# Convert sites to lowercase for proper aggregation
pd.readcsv$Site <- sapply(pd.readcsv$Site, tolower)
# Get vector of unique site names
sites <- unique(pd.readcsv$Site)
# Create aggregate dataframe with total times per site
pd.SumSiteTimes <- aggregate(Times~Site, sum, data = pd.readcsv)
# Create vector for colors
colors <- c("Blue","Green","Yellow","Brown","Green")
# Create pie chart
pie(pd.SumSiteTimes$Times[order(pd.SumSiteTimes$Times,decreasing = T)],
col = colors,
border = NA,
main = "Site Usage")
# Add legend to the plot
legend("topright", sites, cex = .8, fill = colors)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.