Command line correction R in ggplot2

Asked

Viewed 28 times

4

I used the following code to generate the attached chart

ggplot(dados,aes(x = Espécies, y = DNASat, size = Reads, fill=Espécies)) + 
  scale_y_discrete(limits = positions) + 
  geom_point(shape = 21) + 
  theme_bw() + 
  scale_fill_brewer(palette="Pastel1") + 
  scale_size_area(max_size=13)

In that code I used the function scale_y_discrete(limits = positions) to sort the data on the y-axis as it is on the graph, however, as you can see, I lost half of the useful space of this graph when doing this and I don’t know why. Someone knows how to fix it?

In annex the dput(dados)

dados <-
structure(list(DNASat = c("CpaSat1A", "CpurSat2A", "CpurSat3Y", 
"CpurSat4A", "CpurSat5Y", "CpurSat6Z", "CpurSat7Y", "CpurSat8B", 
"CpurSat9Z", "CpurSat10Y", "CameSat1A", "CameSat2B", "CameSat3Y", 
"CameSat4Y", "CpaSat1A", "CpurSat2A", "CpurSat3Y", "CpurSat4A", 
"CpurSat5Y", "CpurSat6Z", "CpurSat7Y", "CpurSat8B", "CpurSat9Z", 
"CpurSat10Y", "CameSat1A", "CameSat2B", "CameSat3Y", "CameSat4Y"
), Espécies = c("Cenchrus purpureus", "Cenchrus purpureus", "Cenchrus purpureus", 
"Cenchrus purpureus", "Cenchrus purpureus", "Cenchrus purpureus", 
"Cenchrus purpureus", "Cenchrus purpureus", "Cenchrus purpureus", 
"Cenchrus purpureus", "Cenchrus purpureus", "Cenchrus purpureus", 
"Cenchrus purpureus", "Cenchrus purpureus", "Cenchrus americanus", 
"Cenchrus americanus", "Cenchrus americanus", "Cenchrus americanus", 
"Cenchrus americanus", "Cenchrus americanus", "Cenchrus americanus", 
"Cenchrus americanus", "Cenchrus americanus", "Cenchrus americanus", 
"Cenchrus americanus", "Cenchrus americanus", "Cenchrus americanus", 
"Cenchrus americanus"), Reads = c(35629, 33216, 31812, 30664, 
7534, 7128, 6395, 1887, 1069, 272, 31857, 1865, 1435, 18, 28201, 
25900, 25967, 25987, 0, 11419, 0, 11879, 0, 0, 26206, 11887, 
336, 220)), class = "data.frame", row.names = c(NA, -28L))

and dput(positions)

positions <-
c("CameSat4Y", "CameSat3Y", "CameSat2B", "CameSat1A", "CpurSat10Y", 
"CpurSat9Z", "CpurSat8B", "CpurSat7Y", "CpurSat6Z", "CpurSat5Y", 
"CpurSat4A", "CpurSat3Y", "CpurSat2A", "CpaSat1A", "CameSat4Y", 
"CameSat3Y", "CameSat2B", "CameSat1A", "CpurSat10Y", "CpurSat9Z", 
"CpurSat8B", "CpurSat7Y", "CpurSat6Z", "CpurSat5Y", "CpurSat4A", 
"CpurSat3Y", "CpurSat2A", "CpaSat1A")  

gráfico

  • Makes a mistake: Error in ggproto(NULL, super, call = match.call(), aesthetics = aesthetics, : object 'positions' not found.

  • Sorry, I ordered the dput data only. I put the correction.

1 answer

5


The problem is the repetition of values in the vector positions. There are only 14 values all of them repeated. So the argument limits Reserve space for 14*2 positions on the axis of y.

I believe the most natural way is to transform the vector DNASat factor with the levels defined by the order of values in positions. But for that I’ll just keep the repeated, because it’s the second half of positions defining the desired order.

library(ggplot2)
library(dplyr)

dados %>%
  mutate(DNASat = factor(DNASat, levels = positions[duplicated(positions)]))%>%
  ggplot(aes(x = Espécies, y = DNASat, size = Reads, fill = Espécies)) +
  geom_point(shape = 21) +
  theme_bw() +
  scale_fill_brewer(palette="Pastel1") +
  scale_size_area(max_size=13)

inserir a descrição da imagem aqui

  • I understood, but note that using this command the order of the Y-axis is changed in relation to the graph I created.

  • 1

    @Alexsilvestrini Finally! See now.

  • It worked out, man. Thank you so much.

Browser other questions tagged

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