How to adjust data labels in base R plot ?


here is my code:

irrigacje <- structure(list(irrigationes = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), levels = c("Control", "Irrigated 10\nmm",
"Irrigated 20 mm"), class = "factor", contrasts = structure(c(0,
1, 0, 0, 0, 1), dim = 3:2, dimnames = list(c("Control", "Irrigated 10\nmm",
"Irrigated 20 mm"), c("Irrigated 10\nmm", "Irrigated 20 mm")))),
    biomass = 1:30), row.names = c(NA, -30L), class = "data.frame")

library(gplots)	# for plotmeans()

plotmeans(biomass ~ irrigationes, data = irrigacje, connect = FALSE, digits = 2,
mean.labels = TRUE, main = "Group means", xlab="Group", ylab = "Mean")

plotmeans(biomass ~ irrigationes, data = irrigacje, connect = TRUE, digits = 2,
mean.labels = TRUE, col="red", pch=7, cex = 2, pos = 2, offset = 1.5, ps = 3, cex.lab=2,
 main = "Group means", xlab="Group", ylab = "Mean", text(biomass, irrigationes, pos = 2))

And this first plot look rather OK, apart from that means' labels overlapping circles representing those means. This is not nice.

and some strange things happen later:

I do not know , what I have done to place this vertical line with some scale on it, in the middle of the plot ?

or somehow I managed to place the same line above X axis. I just want to get rid of it and ask how to adjust
text labels for group means horizontally and vertically. In ggplot we have geom_text and geom_labels but here, I played with pos argument and offset but rather intuitively by trial and error.
Maybe someone could shed some light how to do it properly, thank you.

I would like to know how to change size of pch shapes and what ps argument is for ? I must admit I forgot a bit base R plots lately, but it turns out that it is still needed sometimes.

That's the pos = 2. Leave it to NULL to have the normal axes. If I'm not mistaken, it's used by text() (to indicate where to write the text), but somehow affects the axes placement here.

You can find that in ?par (here for a web version)

integer; the point size of text (but not symbols). Unlike the pointsize argument of most devices, this does not change the relationship between mar and mai (nor oma and omi).
What is meant by ‘point size’ is device-specific, but most devices mean a multiple of 1bp, that is 1/72 of an inch.

Either an integer specifying a symbol or a single character to be used as the default in plotting points. See points for possible values and their interpretation. Note that only integers and single-character strings can be set as a graphics parameter (and not NA nor NULL).
Some functions such as points accept a vector of values which are recycled.

This might be a case where, to get more control, you might want to go to the source code of plotmeans() (press F2 with the cursor in the function name), and maybe do some selective copy/pasting to adapt it to your needs. Indeed you'll see it's a wrapper for plotCI() (maybe in your case you're better off calling it directly?) which itself is "just" a collection of calls to arrows() and points(), so if you have a particular design in mind you can use them as a guide to write your own function.

Thank you,
so what would be the final code to rectify this plot ?
I almost recreated that plot in ggplot, but still interested in recalling base R plot code, which for me seems
to be more difficult than ggplot.

That would depend on the plot you're trying to make. It would take some work indeed, probably as much as recreating it with ggplot.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.