0

I am plotting my data in ggplot and am trying to order x-axis of my plot but it doesn't work and I cant understand why. Here is my data test.txt

V1  V2     Count
a   a1b15   2
a   a1b8    7
a   a3b9    9
a   u1b10   11
a   a2b19   2
b   a5b12   1
b   a1b13   5
b   a1b14   10
b   a1b17   15
c   a1b16   16
c   a1b17   17
a   a1b18   5
a   a2b11   10
d   a1b20   15
d   a1b25   16
plotd<-read.table("test.txt", header=TRUE)
ggplot(plotd,aes(x=fct_relevel(V2, "u1b10","a1b13","a1b14","a1b15","a1b16","a1b17","a1b17","a1b18","a1b20","a1b25","a1b8","a2b11","a2b19","a3b9","a5b12"), y=V1)) + 
       geom_tile(aes(fill=Count),  width=0.3) + 
       geom_text(aes(label = Count), data=cbind(aggregate(Count~V1, plotd, sum), V1="Total"), size=10) + 
        geom_text(aes(label = Count), data=cbind(aggregate(Count~V2, plotd, sum), V2="Total"), size=10) + 
        scale_fill_gradient(low = "red", high = "dark red") + 
        theme(axis.text.x = element_text(angle = 60, hjust = 1), text=element_text(size=30)) + 
        scale_y_discrete(name="test", limits = rev(c(unique(as.character(plotd$V1)), 'Total')))+
        scale_x_discrete(limits = c(unique(plotd$V2), "Total")) + 
        labs(x="Med", fill ="Count", Title= "med-test") +
        geom_text(aes(label=plotd$Count), size=8)
Dave2e
  • 22,192
  • 18
  • 42
  • 50
akang
  • 566
  • 2
  • 15
  • 1
    The issue is that you relevel the factor "on the fly" (within `aes()`, not assigning the change to the data frame), but in other layers you specifically refer back to the `plotd` data frame which is still in the default order. Use `plotd$V2 <- fct_relevel(plotd$V2, ...)` as a separate step before plotting, and I think it should work. – Gregor Thomas Feb 18 '21 at 19:40
  • 1
    Also, if you don't want to type out all those levels, there are functions like `gtool::mixedsort()` that will generate that ordering for you. [See this FAQ for details](https://stackoverflow.com/q/17531403/903061). – Gregor Thomas Feb 18 '21 at 19:41
  • Oh---one more issue - `scale_x_discrete(limits = c(unique(plotd$V2), "Total"))` is explicitly setting the axis breaks in the reverse order that they appear in the data frame. Use the same order there (and if you use it there, you don't really need it anywhere else) – Gregor Thomas Feb 18 '21 at 19:44
  • plotd$V2 <- fct_relevel(plotd$V2, ...)-Doesnt work. Removing the scale_x_discrete would solve the problem BUT will not always place the "total" at the very end. – akang Feb 18 '21 at 19:47
  • I edited the data file to reflect the change. – akang Feb 18 '21 at 19:54
  • scale_x_discrete(limits = c(unique(plotd$V2), "u1b10","a1b13","a1b14","a1b15","a1b16","a1b17","a1b17","a1b18","a1b20","a1b25","a1b8","a2b11","a2b19","a3b9","a5b12", "Total")) does work .BUT this all is pushed to the right side of the plot while the still contains the default order on the left (empty tiles) – akang Feb 18 '21 at 20:02
  • 1
    Get rid of the `unique(plotd$V2)` in your limits if you explicitly write them out. `scale_x_discrete(limits = c("u1b10","a1b13","a1b14","a1b15","a1b16","a1b17","a1b17","a1b18","a1b20","a1b25","a1b8","a2b11","a2b19","a3b9","a5b12", "Total"))` – Gregor Thomas Feb 18 '21 at 20:11
  • Worked perfectly! Thanks – akang Feb 18 '21 at 20:15

0 Answers0