r - ggplot2 graphic does not match the label -
i'm having trouble making charts in order appears in axis.
i changed levels of factors in every possible way (relevel, level, ...). when plot main graphs, in correct order, labels in correct order too, boxplots in alphabetical order , content of 1 main graph switched (alphabetical order too).
the code below works well:
a <- ggplot(data, aes(x = groupx, y = score, fill=groupx, order=groupx))+ scale_fill_manual(values=colours) + theme(axis.text.x = element_text(angle = 45, hjust = 1), legend.position="none", panel.background = element_rect(fill = "white"), panel.grid.major = element_line(colour = "gray95"), panel.border = element_rect(fill = na, colour = "black", size = 2))+ ylab("lesion")+ xlab("")+ guides(colour=false)+ facet_wrap(~ portion)
the code below produces correct facet:
when try plot with:
a + geom_boxplot()
the order of bars , entire graph appears in alphabetical order.
levels in correct format. can't figure how correct this.
- reproducible example
dataset:
structure(list(level = structure(c(1l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l), .label = c("a", "b", "c", "d", "e" ), class = "factor"), factor = structure(c(1l, 1l, 3l, 3l, 2l, 2l, 2l, 1l, 1l, 1l, 2l, 2l, 2l, 3l, 3l, 1l, 1l, 1l, 2l, 2l, 2l, 3l, 3l, 1l, 1l, 1l, 2l, 2l, 2l, 3l, 3l, 3l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 3l, 3l, 3l, 3l, 3l, 3l), .label = c("x", "y", "z" ), class = "factor"), valor = c(12, 11, 20, 15, 2, 5, 21, 21, 51, 1, 2, 15, 2, 56, 5, 21, 5, 12, 21, 15, 23, 5, 4, 55, 1, 2, 89, 2, 4, 12, 23, 10, 12, 51, 45, 2, 15, 32, 21, 15, 4, 5, 45, 45, 2, 14)), .names = c("level", "factor", "valor"), row.names = c(na, -46l), class = "data.frame")
code without change order:
a <- ggplot(data, aes(x = level, y = valor, fill=factor, order=level)) + facet_wrap(~ factor) + geom_boxplot()
when set factor - fast way:
levels(data$factor) <- c("y", "z", "x") levels(data$level) <- c("d", "c", "a", "b", "e")
labels changed, plot don't change.
you try change order of levels
levels(data$factor) <- c("y", "z", "x") levels(data$level) <- c("d", "c", "a", "b", "e")
however, change names of levels. first level before called "y", still first level , there still same data associated it. can redefine factor change order. should give expected result:
data$factor <- factor(data$factor, c("y", "z", "x")) data$level <- factor(data$level, c("d", "c", "a", "b", "e"))
let me show effect has. first, plot data is:
then, redefine factors , plot again:
data$factor <- factor(data$factor, c("y", "z", "x")) data$level <- factor(data$level, c("d", "c", "a", "b", "e")) ggplot(data, aes(x = level, y = valor, fill=factor)) + facet_wrap(~ factor) + geom_boxplot()
Comments
Post a Comment