Reputation: 23587
Given the following example and chart, how would one be able to plot the exact value of the x axis next to each points?
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)
I tried modifying the labels argument, but it only adjusts the y axis label name.
Upvotes: 2
Views: 1837
Reputation: 145965
You'll need to sort by category, then by x. Then you can use text
as Ricardo suggests, accounting for the breaks between categories.
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)
Upvotes: 2