0

I have a Problem with a Plot I want to order, but it seems like it cant be.

install.packages("reshape2")
    library(reshape2)
    install.packages("ggplot2")
    library(ggplot2)
    df <- createRegressionTable(data,colname)
    gg <- melt(df, id = "colname")
    return(
      ggplot(gg, aes(
        x = colname, y = variable, fill = value
      )) +
        geom_tile(show.legend = FALSE) +
        geom_text(aes(label = value), alpha = 0.6) +
        scale_fill_gradient(low = "#D5E8D4", high = "#F8CECC") +
        labs(
          x = "Regressant",
          y = "Regressor"
        ) +
        theme(legend.key = element_blank())
    )

I know the function createRegressionTable is a black box but this is the result:

list(colname = c("zielrichtungU", "zielrichtungO", 
"imitationU", "imitationO", "steuerungU", "steuerungO", "neuheitU", 
"neuheitO", "netzwerkU", "netzwerkO"), zielrichtungU = c(5, 1, 
5, 1, 3, 4, 1, 1, 1, 1), zielrichtungO = c(1, 5, 1, 5, 1, 5, 
3, 5, 1, 1), imitationU = c(5, 1, 5, 5, 1, 5, 1, 1, 4, 1), imitationO = c(1, 
5, 5, 5, 1, 1, 5, 5, 5, 5), steuerungU = c(3, 1, 1, 1, 5, 5, 
1, 2, 1, 1), steuerungO = c(4, 5, 5, 1, 5, 5, 3, 5, 1, 3), neuheitU = c(1, 
3, 1, 5, 1, 3, 5, 5, 1, 1), neuheitO = c(1, 5, 1, 5, 2, 5, 5, 
5, 1, 1), netzwerkU = c(1, 1, 4, 5, 1, 1, 1, 1, 5, 5), netzwerkO = c(1, 
1, 1, 5, 1, 3, 1, 1, 5, 5))

I tested whether the output of melt is scrambled, but it seems to be ordered, as I wished, and now I don't know where the problem lies

And here is the Plot, that I'd love to order:

enter image description here

derbersk
  • 21
  • 2
  • 2
    Could you please share your data using `dput` instead of images? – Quinten Aug 11 '22 at 17:45
  • 1
    The most common way to do this in ggplot2 is to turn your variable into an ordered factor. There are many answers on this forum for that question if you search for, e.g. "[ggplot2] reorder x axis" or similar. The `forcats` package offers a nice wrapper around base R functions for creating and manipulating ordered factors. – Jon Spring Aug 11 '22 at 17:49

0 Answers0