简体   繁体   中英

Plot 3 categories of dots with single x axis scale - R ggplot2

I want to visualize how does the length of a sequence affect numbers of errors in different categories.

The problem actually starts from the file formatting, but having a file formatted as follows:

category count length
s 12 1500
i 13 1500
d 15 1500
s 17 1600
i 18 1600
d 22 1600
s 14 1500
i 15 1500
d 17 1500
s 30 1800
i 40 1800
d 0 1800

How can I plot this to have length column as x-axis, s,i,d as dots of different colours or shapes.

I'm not sure why you accepted the above answer because you've asked specificly for ggplot2. So I'm adding the ggplot solution just in case

library(ggplot2)
test <- data.frame(category = factor(c("s","i","d","s","i","d","s","i","d","s","i","d")),
                   count = c(12,13,15,17,18,22,14,15,17,30,40,0),
                   length = c(1500,1500,1500,1600,1600,1600,1500,1500,1500,1800,1800,1800))

ggplot(test, aes(x = length, y = count)) + geom_point(aes(colour = category))

Perhaps this works:

xx = read.table("/tmp/tmp.txt", sep=" ", header=TRUE)
yy = split(xx[c("count", "length")], xx[["category"]])
with(yy[["d"]],
     plot(y=count, x=length,
          xlim=c(min(xx[["length"]]), max(xx[["length"]])),
          ylim=c(min(xx[["count"]]), max(xx[["count"]])),
          col="red"))
with(yy[["i"]], points(y=count, x=length, col="blue"))
with(yy[["s"]], points(y=count, x=length, col="green"))

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