简体   繁体   中英

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

R newbie here, so you're probably going to judge but here's my question.

I have a very simple structured CSV file that has 2 columns: labels (ASCII text values) as column 1 and their respective counts (numerical) as column 2.

For example, the CSV is of the format:

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

I want to plot a simple line graph in R that has 'counts' as y-axis and labels on x-axis. I wish to actually be able to see the 'labels' such as 'dog' 'cat' marked on the x-axis or data-point. How do I do this in R?

Here's what I have so far:

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

It produces an empty graph with improper labels.

Roughed out in base R graphics. Look at axis , mtext and plot options to refine.

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

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

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

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

using ggplot2

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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