简体   繁体   中英

geom_text gives vertically stacked labels to dot plot

I've been trying to create a dot plot with numbers inside each dot. Some of the points are close horizontally to one another (just barely intersecting), and their numerical labels appear vertically stacked in between the two points. I attached a photo of the graph.

I have tried adjusting all of the arguments of geom_dotplot and geom_text to no luck.

My code is as follows:

g <- DATA_TABLE %>%
  ggplot(aes(x = factor(X_VAR), y = AVG, fill = factor(COLOR_VAR))) +
  geom_dotplot(binaxis = "y", stackdir = "center",
               position = "dodge", binwidth = .1) +
  geom_text(mapping = aes(label = factor(LABEL_VAR)),
            position = position_dodge(.9), size = 5.5)
print(g)

Here's a subset of the data.table:

X_VAR, LABEL_VAR, AVG, COLOR_VAR
A, 3, 2.206897, blue
B, 3, 1.896552, blue
C, 3, 2.034483, blue
A, 4, 2.727273, green
B, 4, 2.545455, green
C, 4, 2.636364, green
A, 4, 2.125000, blue
B, 4, 1.875000, blue
C, 4, 2.187500, blue

我遇到问题的图表

This isn't a very pretty solution, but it gets you down the road toward what I think you're after.

The problem isn't with the geom_text() layer - in fact, the locations of the text labels are where they're supposed to be, according to the x/y coordinates you provide in the top level mapping. The issue instead is that geom_dotplot() bins your observations (in this case, along the y axis), and then plots those dots in the middle of the bin. So the trick is to provide x/y coordinates for the labels that match where the dots end up after the binning (and in your case, dodging) calculations.

So how to find the coordinates of the dots?

You'll note from the geom_dotplot() help page that x and y are computed variables. You can find these by accessing the ggplot_build() object for the dotplot itself:

g <- DATA_TABLE %>%
  ggplot(aes(x = factor(X_VAR), y = AVG, fill = factor(COLOR_VAR))) +
  geom_dotplot(binaxis = "y", stackdir = "center",
               position = "dodge", binwidth = 0.1)

plot_data <- ggplot_build(g)$data[[1]]

Among other things, you'll find x , y , xmin , and xmax , all of which relate to the centres and the edges of the bins that the dots end up in, and stackpos which describes the stacking order of the dots themselves. You can use these variables to find the centre points of the dots after dodging, and add these to your data like so:

DATA_TABLE <- DATA_TABLE %>%
  arrange(X_VAR) %>%
  mutate(dot_x = plot_data$x+(plot_data$stackpos*(plot_data$xmax-plot_data$x)), dot_y=plot_data$y)

Now you have x/y coordinates for the centre of the dots, which you can use to specify the locations of the text labels:

g + geom_text(data=DATA_TABLE, mapping = aes(x=dot_x, y=dot_y, label = factor(LABEL_VAR)), size = 5.5)

print(g)

在此处输入图像描述

However, this answer falls a bit short because the size of the dots is in relation to binwidth , not to the panel size or dimensions - so as you get to very wide or very narrow plot dimensions, the numbers are no longer in the centre of the dots, despite still being in the "right" place.

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