I was looking for a way to plot using facet_grid in ggplot2 that only displays just a few select facets. say I have the following plot:
Been looking for a quick way to, for instance, just plot facets 1 and 3.
#data
y<-1:12
x<-c(1,2,3,1,2,3,1,2,3,1,2,3)
z<-c("a","a","a","b","b","b","a","a","a","b","b","b")
df<-as.data.frame(cbind(x,y,z))
#plot
a <- ggplot(df, aes(x = z, y = y,
fill = z))
b <- a + geom_bar(stat = "identity", position = "dodge")
c <- b + facet_grid(. ~ x, scale = "free_y")
c
Obviously I figured out how to just chop up my data first but this must of course be possible to allocate in ggplot2 Even just a nudge would be most welcome.
Use subset in your ggplot call.
plot_1 = ggplot(subset(df, x %in% c(1, 2)), aes(x=z, y=y, fill=z)) +
geom_bar(stat = "identity", position = "dodge") +
facet_grid(. ~ x, scale = "free_y")
Would this be okay,
a <- ggplot(subset(df, x != 2), aes(x = z, y = y, fill = z))
b <- a + geom_bar(stat = "identity", position = "dodge")
c <- b + facet_grid(. ~ x, scale = "free_y")
c
Related
Goal is to achieve ordered categories on y axis.
y1 -> y2 -> y3.
Here's the example:
require(data.table)
require(ggplot2)
dt <- data.table(var = rep("x1", 3),
categ = paste0("y", c(1,2,3)),
value = c(-2,0.5,-1))
ggplot(dt, aes(x = categ, y = value)) +
geom_bar(stat = "identity") +
coord_flip() +
theme_bw()
It seems to be reversed. Here's the one way to achieve desired ordering in ggplot2:
dt$categ <- factor(dt$categ, levels = rev(levels(factor(dt$categ))))
ggplot(dt, aes(x = categ, y = value)) +
geom_bar(stat = "identity") +
coord_flip() +
theme_bw()
Great, now ordering seems to be right. But with some modifications:
ggplot(dt, aes(x = categ, y = value)) +
geom_bar(data = dt[value < 0], stat = "identity", fill = "darkred") +
geom_bar(data = dt[value >= 0], stat = "identity", fill = "darkblue") +
coord_flip() +
theme_bw()
For some reason factor ordering is ignored here. Any clues why?
Solution could be:
# dt is original data without factors
ggplot(dt, aes(categ, value, fill = value >= 0)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("darkred", "darkblue")) +
# since we want y1 on top and y3 on bottom we have to apply rev
scale_x_discrete(limits = rev(dt$categ)) +
coord_flip() +
theme_bw()
Trick is to pass dt$categ as limits argument to scale_x_discrete(). In your first plot order is not reversed, this is how it should be as ggplot2 starts putting values from the origin of the axis (0).
I also removed two geom_bar lines that were used in a not-ggplot way.
I first make a plot
df <- data.frame(x = c(1:40, rep(1:20, 3), 15:40))
p <- ggplot(df, aes(x=x, y = x)) +
stat_density2d(aes(fill='red',alpha=..level..),geom='polygon', show.legend = F)
Then I want to change the geom_density values and use these in another plot.
# build plot
q <- ggplot_build(p)
# Change density
dens <- q$data[[1]]
dens$y <- dens$y - dens$x
Build the other plot using the changed densities, something like this:
# Built another plot
ggplot(df, aes(x=x, y =1)) +
geom_point(alpha = 0.3) +
geom_density2d(dens)
This does not work however is there a way of doing this?
EDIT: doing it when there are multiple groups:
df <- data.frame(x = c(1:40, rep(1:20, 3), 15:40), group = c(rep('A',40), rep('B',60), rep('C',26)))
p <- ggplot(df, aes(x=x, y = x)) +
stat_density2d(aes(fill=group,alpha=..level..),geom='polygon', show.legend = F)
q <- ggplot_build(p)
dens <- q$data[[1]]
dens$y <- dens$y - dens$x
ggplot(df, aes(x=x, y =1)) +
geom_point(aes(col = group), alpha = 0.3) +
geom_polygon(data = dens, aes(x, y, fill = fill, group = piece, alpha = alpha)) +
scale_alpha_identity() +
guides(fill = F, alpha = F)
Results when applied to my own dataset
Although this is exactly what I'm looking for the fill colors seem not to correspond to the initial colors (linked to A, B and C):
Like this? It is possible to plot a transformation of the shapes plotted by geom_density. But that's not quite the same as manipulating the underlying density...
ggplot(df, aes(x=x, y =1)) +
geom_point(alpha = 0.3) +
geom_polygon(data = dens, aes(x, y, fill = fill, group = piece, alpha = alpha)) +
scale_alpha_identity() +
guides(fill = F, alpha = F)
Edit - OP now has multiple groups. We can plot those with the code below, which produces an artistic plot of questionably utility. It does what you propose, but I would suggest it would be more fruitful to transform the underlying data and summarize that, if you are looking for representative output.
ggplot(df, aes(x=x, y =1)) +
geom_point(aes(col = group), alpha = 0.3) +
geom_polygon(data = dens, aes(x, y, fill = group, group = piece, alpha = alpha)) +
scale_alpha_identity() +
guides(fill = F, alpha = F) +
theme_minimal()
g = ggplot(Values, aes(x = X, y = Y, fill=factor(Z))) +
geom_bar(width=0.8, stat = "identity", position="dodge") +
facet_grid(Z ~ ., scale = "free_y") +
labs(x="Anno", y = "Riserva Rivalutata") +
theme(legend.position ="none") +
scale_x_continuous(breaks = seq(2000, 2070, by = 5))
d = ggplot(Values, aes(x = X, y = Y, fill=factor(Z))) +
geom_bar(width=0.8, stat = "identity", position="dodge")
p = subplot(g,d, nrows=2, shareX= T,which_layout = 1)
Hello,
I'm creating an object that when I click 2 objects Z, shows me above 2 distinct graphs, while below I would like to see the subtraction of the two.
Right now I can only see them distinct.
Can you help me to make a single chart but with only one bar given by the difference between the two data?
Thank you
library(ggplot2)
set.seed(2)
a = sort(rep(c("A","B"),6))
b = c(rep(1:3,2),rep(4:6,2))
cc = rnorm(length(a))
d = rep(sort(rep(1:2,3)),2)
df = data.frame(a,b,cc,d)
print(df)
ggplot(df, aes(x = as.factor(b), y = cc, fill = as.factor(d))) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(~a)
In the following plot:
How do I get rid of the redundant x-axis values for each of the factors of a i.e. "A" & "B".
I mean the 4:6 are not required for "A" and similarly 1:3 for "B".
What is the tweak I need to do?
facet_wrap and facet_grid both have a scales argument that let you define which of the x and/ or y scales should be free or fixed.
In your case, you want the x dimensions to be free to be different in both facets, therefore
ggplot(df, aes(x = as.factor(b), y = cc, fill = as.factor(d))) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(~ a, scales = 'free_x')
I'm creating a table with a lot of plot using ggplot
a=rnorm(30)
b=a*a
c=rnorm(30)
d=c
l=runif(30)
m=l+3
data=data.frame(A=a,B=b,ss=1)
data=rbind(data,data.frame(A=c,B=d,ss=2))
ggplot()+ geom_line(data=data,aes(A,B,group=ss),col="red")+facet_wrap(~ ss,as.table=T
In each of this plots I have to overlap an histogram.
How can I do?
Here's a way to do it:
ggplot() +
geom_line(data = data, aes(x = A, y = B), col = "red") +
geom_histogram(data = data, aes(x = A), alpha = .5) +
facet_wrap(~ ss,as.table=T)