Plot order scatterplot ggplot2

Asked

Viewed 35 times

1

I’m doing a scatterplot and mapping the points by color (two categories) and size (a discrete variable).

The points must overlap, as they are measured from the same place, in different times

The problem is: the largest point is plotted 'in front' of the smallest, then superimposed. Even using alpha to make 'transparent' is getting very ugly.

Is there any way I can reverse the order that ggplot plots the variables?

Here is a chart print. Ideally, black dots should be plotted behind red dots.

inserir a descrição da imagem aqui

Edit:

LIGHT <-
 structure(list(Sensor = c("Sensor 01", "Sensor 02", 
 "Sensor 03", "Sensor 04", "Sensor 05", "Sensor 06", 
 "Sensor 07", "Sensor 08", "Sensor 09", "Sensor 10", 
 "Sensor 11", "Sensor 12", "Sensor 13", "Sensor 14", 
 "Sensor 15", "Sensor 01", "Sensor 02", "Sensor 03", 
 "Sensor 04", "Sensor 05"), Light = c(29.2326025, 
 19.1888217741935, 20.2879273387097, 22.6006463709677, 
 29.4652291129032, 31.4922126612903, 18.0189039516129, 
 44.2132503225806, 19.6836093548387, 29.6778536290323, 
 23.7034666935484, 37.4008766935484, 32.4513981451613, 
 57.5753566129032, 41.8583696774194, 64.6308373214286, 
 44.2330882142857, 45.8567069642857, 61.8264760714286, 
 57.66113125), Dif_PAR.sensor = c(136.820568709677, 
 146.864349435484, 145.765243870968, 143.45252483871, 
 136.587942096774, 134.560958548387, 148.034267258065, 
 121.839920887097, 146.369561854839, 136.375317580645, 
 142.349704516129, 128.652294516129, 133.601773064516, 
 108.477814596774, 124.194801532258, 81.3658292857143, 
 101.763578392857, 100.139959642857, 84.1701905357143, 
 88.3355353571429), Percent_of_PAR = c(19.0689480982097, 
 14.2716407995484, 15.1293532713629, 15.8380714606935, 
 17.456968456871, 18.0777406968065, 13.7234488349113, 
 72.7551326408065, 11.6857118804435, 21.3044665453226, 
 16.434219278871, 25.781927983129, 23.9310200270565, 
 42.2960034949194, 32.4126078909758, 39.0122370998214, 
 26.402147205, 30.3298732019643, 36.2347200010714, 
 31.7127142146429), Height = c(11.3, 11.65, 12.8, 13.1, 
 13.1, 13.45, 13.5, 15.05, 16.15, 16.5, 17.7, 18.45, 18.5, 
 19, 20.1, 11.3, 11.65, 12.8, 13.1, 13.1), North = c(0, 0, 
 0, 1.5, 3, 0, 0, 0, 1.9, 2.3, 0.4, 0, 4.2, 0, 1, 0, 0, 0, 
 1.5, 3), South = c(0, 0, 0, 0, 0, 0.1, 1.5, 0.7, 0, 0, 0, 
 0, 0, 0.2, 0, 0, 0, 0, 0, 0), East = c(0.3, 0, 1.9, 0, 0.7, 
 0, 0, 0.3, 0, 0.6, 1, 0, 0, 0, 0.2, 0.3, 0, 1.9, 0, 0.7), 
 West = c(0, 0, 0, 0, 0, 2.5, 0.1, 0, 1.9, 0, 0, 2, 0, 0, 
 0, 0, 0, 0, 0, 0 ), Northeast = c(0, 0, 2.45, 1.95, 3.55, 
 0, 0, 0, 0, 2.9, 1.6, 0, 0, 0, 1.5, 0, 0, 2.45, 1.95, 3.55), 
 Northwest = c(0, 0, 0, 0, 0, 0, 0, 0, 3.25, 0, 0, 0, 4.6, 
 0, 0, 0, 0, 0, 0, 0), Southeast = c(0.85, 0, 0, 0, 0, 0, 0, 
 1.3, 0, 0, 0, 0, 0, 0, 0, 0.85, 0, 0, 0, 0), Southwest = c(0, 
 0, 0, 0, 0, 2.95, 1.95, 0, 0, 0, 0, 2.5, 0, 0.8, 0, 0, 0, 0, 
 0, 0), Dist_to_tree_center = c(0.85, 0, 2.45, 1.95, 3.55, 
 2.95, 1.95, 1.3, 3.25, 2.9, 1.6, 2.5, 4.6, 0.8, 1.5, 0.85, 
 0, 2.45, 1.95, 3.55), N_S = c(-0.5, 0, 0.5, 1.9, 3.4, -0.5, 
 -1.9, -1.1, 2.3, 2.7, 0.8, -0.5, 4.6, -0.6, 1.4, -0.5, 0, 0.5, 
 1.9, 3.4), E_W = c(0.7, 0, 2.4, 0.5, 1.1, -2.9, -0.5, 0.7, 
 -2.3, 1, 1.4, -2.4, -0.5, -0.5, 0.6, 0.7, 0, 2.4, 0.5, 1.1), 
 Aspect = c("Southeast", "On the bole", "Northeast", 
 "Northeast", "Northeast", "Southwest", "Southwest", 
 "Southeast", "Northwest", "Northeast", "Northeast", 
 "Southwest", "Northwest", "Southwest", "Northeast", 
 "Southeast", "On the bole", "Northeast", "Northeast", 
 "Northeast"), Leaf_set = c("With", "With", "With", 
 "With", "With", "With", "With", "With", "With", "With", 
 "With", "With", "With", "With", "With", "Without", 
 "Without", "Without", "Without", "Without")), 
 row.names = c(NA, 20L), class = "data.frame")

The code I used was this:

ggplight <- ggplot(LIGHT, aes(x=Dist_to_tree_center, y=Height, size = Percent_of_PAR, color=Leaf_set))+
  scale_color_manual(values=colors, labels=c("With leaves", "Without leaves"))+
  geom_point(alpha=.4) + 
  scale_size(range = c(3, 15), name="Light")+
  
  labs(x = 'Distance to tree centre (m)', y = 'Height (m)', color='', title = '(b)', subtitle = '')+
  
  theme_classic(base_size = 30)+
  theme(panel.background = element_blank())+
  theme(panel.grid.major = element_blank())+
  theme(panel.grid.minor = element_blank())+
  theme(plot.background = element_blank())+
  guides(color = FALSE)+
  theme(legend.position=c(1,0.1), legend.justification=c(1,-0.1))+
  theme(legend.background = element_rect(fill="transparent"))
  • 1

    Can you please, edit the question with the departure of dput(LIGHT) or, if the base is too large, dput(head(LIGHT, 20))?

  • See if that’s it, please.

  • Yes that’s right, thank you.

  • However, with the question data, there are no points of different colors superimposed.

  • I’m sorry if you’re confused. What I did was: I used "Height" on the y-axis and "Dist_to_tree_center" on the x-axis. the plotted values mapped with size are those of "Percent_of_par" and used color to map "Leaf_set".

1 answer

2


One way to plot the smallest points over the largest ones is to notice that the smallest points correspond to the Leaf_set == "With" and use two geom_point, filtering the Leaf_set corresponding to each of the values. First the "Without" and then the "With".

library(ggplot2)

ggplight <- ggplot(LIGHT, aes(Dist_to_tree_center, Height, size = Percent_of_PAR, color = Leaf_set))+
  geom_point(
    data = subset(LIGHT, Leaf_set == "Without"),
    alpha = 0.6
  ) +
  geom_point(
    data = subset(LIGHT, Leaf_set == "With"),
    alpha = 0.6
  ) +
  scale_color_manual(breaks = c("With", "Without"), values = colors) +
  scale_size(range = c(3, 15), name="Light")+
  labs(x = 'Distance to tree centre (m)', y = 'Height (m)', color='', title = '(b)', subtitle = '') +
  guides(color = "none") +
  theme_Mariana()

ggplight

inserir a descrição da imagem aqui


theme of the graph

theme_Mariana <- function(){ 
    theme_classic() + 
    theme(
        title = element_text(face = "bold"),
        axis.title = element_text(face = "bold"),
        panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        plot.background = element_blank(),
        legend.title = element_text(face = "bold"),
        legend.text = element_text(face = "bold"),
        legend.position = c(1, 0.05), 
        legend.justification = c("right", "bottom"),
        legend.background = element_rect(fill = "transparent")
    )
}
  • I will run my script here and confirm the data, just a moment, please.

  • Ah, I think I understand what happened. When I sent the output of "dput(head(LIGHT, 20))" did not take the entire data set, so the data referring to some overlaps were missing. But some left (the 5 points at the bottom). That’s exactly what happens on my chart. I wanted a way to plot the larger circle underneath, reversing the order in which they overlap.

  • @Mariana The previous codex was wrong, I changed the colors and could not see anything wrong. See now.

  • That’s right! What have you changed? the/

  • Aaahh, I found now what you wrote!! Sorry, first time using here. Thank you, Rui!! A great Sunday!

  • @Mariana What to do when someone answers to your questions.

  • Ahhh, got it! I think I did it right, but if I’m wrong, tell me I’ll change. Oh, again, thank you so much!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.