标记点图

时间:2013-05-14 20:05:33

标签: r

鉴于以下示例和图表,如何能够在每个点旁边绘制x轴的精确值?

x <- mtcars[order(mtcars$mpg),] # sort by mpg
x$cyl <- factor(x$cyl) # it must be a factor
x$color[x$cyl==4] <- "red"
x$color[x$cyl==6] <- "blue"
x$color[x$cyl==8] <- "darkgreen"    
dotchart(x$mpg,labels=row.names(x),cex=.7,groups= x$cyl,
 main="Gas Milage for Car Models\ngrouped by cylinder",
   xlab="Miles Per Gallon", gcolor="black", color=x$color)

我尝试修改labels参数,但它只调整y轴标签名称。

1 个答案:

答案 0 :(得分:2)

您需要按类别排序,然后按x排序。然后你可以像里卡多建议的那样使用text,考虑类别之间的中断。

x <- mtcars[order(-mtcars$cyl, mtcars$mpg),]
# sort by category, then by position within category

# As above
x$cyl <- factor(x$cyl) # it must be a factor
x$color[x$cyl==4] <- "red"
x$color[x$cyl==6] <- "blue"
x$color[x$cyl==8] <- "darkgreen"    
dotchart(x$mpg,labels=row.names(x),cex=.7,groups= x$cyl,
         main="Gas Milage for Car Models\ngrouped by cylinder",
         xlab="Miles Per Gallon", gcolor="black", color=x$color)

# Adding text
text(x = x$mpg,
     y = 1:nrow(x) + ifelse(x$cyl == "6", 2, ifelse(x$cyl == "4", 4, 0)),
     labels= x$mpg,
     cex = 0.5,
     pos = 4)