Harpal
Harpal

Reputation: 12587

Multiple values on each line with dotchart in R

I have the following input file for R:

car    1
car    2 
car    3 
car2   1 
car2   2 
car2   3 

I then use the following commands to plot my graph:

autos_data <- read.table("~/Documents/R/test.txt", header=F)

dotchart(autos_data$V2,autos_data$V1)

But this plots each car and car2 value on a new line, how can I plot the chart so that all the car values are on one line and all the car2 values are on another line.

Upvotes: 5

Views: 4545

Answers (3)

Whitebeard
Whitebeard

Reputation: 6203

Here's a ggplot2 solution

qplot(V1, V2, data=autos_data) + coord_flip()

enter image description here

Upvotes: 1

gung - Reinstate Monica
gung - Reinstate Monica

Reputation: 11903

Note that you can add dots to a dotchart with ?points, so this can be done in base R with a little bit of data management. Here's how it could be done:

autos_data = read.table(text="car    1
car    2 
car    3 
car2   1 
car2   2 
car2   3", header=F)

aData2 = autos_data[!duplicated(autos_data[,1]),]

dotchart(aData2[,2], labels=aData2[,1], 
         xlim=c(min(autos_data[,2]), max(autos_data[,2])))
points(autos_data[,2] , autos_data[,1])

enter image description here

@Josh O'Brien's lattice solution is more elegant, of course.

Upvotes: 5

Josh O&#39;Brien
Josh O&#39;Brien

Reputation: 162401

As far as I can tell, there's just no way to do that with base dotchart.

However, if a lattice dotplot suits your needs as well, you can just do:

library(lattice)
dotplot(V1~V2, data=autos_data)

enter image description here

Upvotes: 6

Related Questions