繁体   English   中英

如何在R中绘制包含标签及其计数的CSV文件?

[英]How to plot a CSV file containing labels and their counts in R?

R新手在这里,所以您可能要去判断,但这是我的问题。

我有一个非常简单的结构化CSV文件,其中包含2列:标签(ASCII文本值)作为第1列,它们各自的计数(数字)作为第2列。

例如,CSV的格式为:

type,count
cat,23000
dog,444566,
wolf,3442
tiger,306
...

我想在R中绘制一个简单的折线图,其中“计数”为y轴,标签为x轴。 我希望实际上能够看到在x轴或数据点上标记的“标签”,例如“狗”“猫”。 如何在R中执行此操作?

这是我到目前为止的内容:

> heresmydata <- read.csv("data.csv")
> matplot(heresmydata[, 1], heresmydata[, -1], type="l")
Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log = log) :
  NAs introduced by coercion
2: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
> 

它会生成带有不正确标签的空图。

在基础R图形中粗糙化。 查看axismtextplot选项进行优化。

读入: data <- read.csv("data.csv", header=TRUE, stringsAsFactors=FALSE)

绘图: plot(data$count, type="l", axes=FALSE, ylim=c(min(data$count), max(data$count)), xlab="Creature", ylab="Count")

Y轴: axis(side=2, at=c(min(data$count), max(data$count)), labels=c(min(data$count), max(data$count)))

X轴: axis(side=1, at=seq(1,nrow(data),1), labels=data$type)

使用ggplot2

library("ggplot2")
ggplot(heresmydata, aes(x = type, y = count)) + 
  geom_bar(stat = "identity") +
  scale_y_log10()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM