Stack Overflow em Português Asked by Alex Silvestrini on September 27, 2021
Utilizei o seguinte código para gerar o gráfico em anexo
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)
Nesse código utilizei a função scale_y_discrete(limits = positions)
para ordenar o dados no eixo Y como estão no gráfico, porém, como dá para perceber, perdi metade do espaço útil desse gráfico ao fazer isso e não sei o motivo. Alguém sabe como arrumar isso?
Em anexo o 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))
e 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")
O problema está na repetição de valores no vetor positions
. Só há 14 valores todos eles repetidos. Então o argumento limits
reserva espaço para 14*2 posições no eixo dos y
.
Creio que a maneira mais natural é transformar o vetor DNASat
em fator com os níveis definidos pela ordem de valores em positions
. Mas para isso vou ficar só com os repetidos, porque é a segunda metade de positions
que define a ordem pretendida.
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)
Correct answer by Rui Barradas on September 27, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP