Pregunta

This is the current version of my figure:

    require(MuMIn)
    require(ggplot2)
    data(Cement)
    d <- data.frame(Cement)
    dd <- melt(d,id.var = "y")

ggplot(dd,aes(x = y,y = value, group = variable)) + 
  geom_point(size = 2)  + 
  theme_classic() +
  facet_grid(variable ~ ., scales = "free") + 
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
  xlab("") +
  ylab("") +
  guides(colour = FALSE)

enter image description here

How can I

1) Alter this graph so that the X1, X2, X3, and X4 labels are on the left hand side and they read c("factor x^2","factor x^3","factor x^4","factor x^5"),

2) Is there a method for surrounding each panel with a box, to make them more distinguishable?

¿Fue útil?

Solución

Try this,

library(ggplot2)
library(gtable)

p <- ggplot(mtcars, aes(mpg, cyl))+
  facet_grid(gear~., labeller=label_both) + geom_point() +
  theme(strip.text.y=element_text(angle=90)) + labs(y="")


g <- ggplotGrob(p)

g$layout[g$layout$name == "strip-right",c("l", "r")] <- 2
grid.newpage()
grid.draw(g)

Otros consejos

A solution for question 1 (partial) and 2:

names(d) <- c("x^1","x^2","x^3","x^4","y")
dd <- melt(d,id.var = "y", variable.name="factor")

ggplot(dd, aes(x = y, y = value, group = factor)) + 
  geom_point(size = 2)  + 
  theme_bw() +
  facet_grid(factor ~ ., scales = "free", labeller = label_both) + 
  theme(axis.text.x = element_text(angle = 90, hjust = 1), panel.grid = element_blank()) + 
  xlab("") +
  ylab("") +
  guides(colour = FALSE)

which gives: enter image description here

Using labeller is a good option (problem #1) and facet_grid and facet_wrap have a switch argument for moving facet labels around a bit (problem #2):

library("ggplot2")
x <- runif(100)
exp <- rep(1:4, each = 25)
y <- x^exp
df <- data.frame(x, y, exp)

# facet_grid
ggplot(df, aes(x, y)) +
facet_grid(exp ~ ., labeller = label_bquote(factor~x^.(exp)), switch = "y") + 
geom_point() + labs(y="") +
theme(strip.background = element_blank()) # Remove facet border if you want

# facet_wrap
ggplot(df, aes(x, y)) +
facet_wrap(~ exp, ncol = 1, labeller = label_bquote(factor~x^.(exp)), switch = "y") + 
geom_point() + labs(y="") +
theme(strip.background = element_blank())
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top