Define x-axis in combined ggplot

52 Views Asked by At

I made two separated ggplot geom_points, and I combined them to one figure.

  • One with the red dots, with the x-axis ordered from the highest percentage to the lowest percentage
  • One with the blue dots

This is a picture of the figure with only the red dots: enter image description here

This is the picture of the combined figure: enter image description here

After I combined the red with the blue dots, I want to use the order of the x-axis from the figure with the red dots. I tried a lot of combinations, but I could not fixed it. Does anybody know how to fix that?

This is the code of the ggplot:

p <- ggplot(Antibiotica6, 
       aes(x = fct_reorder(Specialisme, Ranking), 
           y = Percentage)) + 
  geom_point(
    aes(size = Frequentie),
    alpha = 0.5,
    colour = "red") +
  scale_y_continuous(
    labels = scales::percent,
    breaks = seq(0, 1, by = 0.1)) +
  scale_size(
    range = c(1, 13), 
    breaks = c(1,50,100,200,300,400,500,600,700,800)) +
  xlab("Specialisme") +
  theme(
    plot.title = element_text(hjust = 0.5),
    plot.subtitle = element_text(hjust = 0.5))

print(p)

q <- p + 
  
  geom_point(data = Antibiotica7, 
    aes(size = Frequentie),
    alpha = 0.5,
    colour = "blue") 

print(q)

This is a part of the data:

Antibiotica 6
Specialisme  Frequentie  Percentage  Ranking   
RTH          1           100         1
GYF          2           100         2
KNO          79          90          3
QKN          54          88          4
CAR          125         77          5

Antibiotica 7
Specialisme  Frequentie  Percentage  Ranking   
RTH          3           100         1
GYF          3           100         2
KNO          188         73          3
CAR          75          65          4
QKN          18          64          5
1

There are 1 best solutions below

1
On

You did not provide a reproducible data. Can you try switching the order in which you render the plots?

p <- ggplot(Antibiotica7, 
            aes(x = fct_reorder(Specialisme, Ranking), 
                y = Percentage)) + 
  geom_point(
    aes(size = Frequentie),
    alpha = 0.5,
    colour = "blue") +
  scale_y_continuous(
    labels = scales::percent,
    breaks = seq(0, 1, by = 0.1)) +
  scale_size(
    range = c(1, 13), 
    breaks = c(1,50,100,200,300,400,500,600,700,800)) +
  xlab("Specialisme") +
  theme(
    plot.title = element_text(hjust = 0.5),
    plot.subtitle = element_text(hjust = 0.5))

print(p)

q <- p + 
  
  geom_point(data = Antibiotica6, 
             aes(size = Frequentie),
             alpha = 0.5,
             colour = "red") 

print(q)