简体   繁体   中英

R: how to increase the distance between label and boxplot

boxplot(count ~ spray, data = InsectSprays, col = "lightgray", names = c("Apple \n Sauce", "Banana", "Candy", "Dandelion", "Eve", "Fox"))

The above code gives me:

在此处输入图片说明

I have a multi-line label, "Apple Sauce", but I don't know how to space it correctly so that the label is not overlaid on top of the boxplot. How can I increase the distance between the label and the boxplot?

Edit: Suppose I want to remove one of the boxplots

newInsectSprays <- InsectSprays[1:60,]
boxplot(count ~ spray, data=newInsectSprays, col="lightgray", xaxt="n")
axis(1, axTicks(1), labels=F)
mtext(c("Apple\nSauce", "Banana\n", "Candy\n", "Dandelion\n", "Eve\n"), 
      1, 2, at=axTicks(1))

This code repeats the "Apple Sauce" label. How do I fix this? 在此处输入图片说明

Edit 2:

newInsectSprays <- InsectSprays[1:60,]
boxplot(newInsectSprays$count[newInsectSprays$spray == "A"], 
        newInsectSprays$count[newInsectSprays$spray == "B"],
        newInsectSprays$count[newInsectSprays$spray == "C"],
        col="lightgray", xaxt="n")
mtext(c("Apple\nSauce", "Banana\n", "Candy\n"), 
      1, 2, at=axTicks(1))

在此处输入图片说明

Building a custom x axis. We may want to avoid spaces, and add \\n to the end of the other strings too, to get a cleaner result.

boxplot(count ~ spray, data=InsectSprays, col="lightgray", xaxt="n")
axis(1, axTicks(1), labels=F)
mtext(c("Apple\nSauce", "Banana\n", "Candy\n", "Dandelion\n", "Eve\n", "Fox\n"), 
      1, 2, at=axTicks(1))

在此处输入图片说明

Edit:

spray is a factor column and one factor is no longer needed, I'd do drolevels .

newInsectSprays <- InsectSprays[1:60,]
boxplot(count ~ spray, data=transform(newInsectSprays, spray=droplevels(spray)), 
        col="lightgray", xaxt="n")
mtext(c("Apple\nSauce", "Banana\n", "Candy\n", "Dandelion\n", "Eve\n"), 
      1, 2, at=axTicks(1))

在此处输入图片说明

Edit2

boxplot(count ~ droplevels(spray), 
        newInsectSprays[newInsectSprays$spray %in% c("A", "B", "C"), ], 
        xaxt="n", xlab="spray")
mtext(c("Apple\nSauce", "Banana\n", "Candy\n"), 1, 2, at=1:3)

在此处输入图片说明

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