user1883491 - 11 months ago 79

R Question

- I want to plot first figure with no NAs and order by median. I think I could do this by using na.omit and the code1 below.
- How can plot the second figure by keeping the ones that are in figure1 (carA and carC) and also keep the same ordering as in figure 1.

a

`R G T`

EG1 2 20 carA

EG2 4 40 carA

EG3 8 80 carA

EG4 10 100 carA

EG5 NA 20 carB

EG6 NA 40 carB

EG7 NA 50 carB

EG8 NA 90 carB

EG9 4 50 carC

EG10 8 60 carC

EG11 12 70 carC

EG12 24 90 carC

code1

`ggplot(a, aes(reorder(T, log2(R+1), median, order=TRUE), log2(R+1), fill=T)) + geom_boxplot()+coord_flip()`

code2

`ggplot(a, aes(reorder(T, log2(G+1), median, order=TRUE), log2(G+1), fill=T)) + geom_boxplot()+coord_flip()`

Answer Source

You need to capture the order from the first plot, and use it to set the limits in the second using `scale_x_discrete`

(due to `coord_flip`

).

```
limits <- levels(reorder(a$T, log2(a$R + 1)))
ggplot(a, aes(reorder(T, log2(R + 1), median, order = TRUE), log2(R + 1), fill = T)) +
geom_boxplot() +
coord_flip() +
scale_x_discrete(limits = limits)
ggplot(a, aes(reorder(T, log2(G + 1), median, order = TRUE), log2(G + 1), fill = T)) +
geom_boxplot() +
coord_flip() +
scale_x_discrete(limits = limits)
```