簡體   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