geom_bar ggplot2 subtract two Values - r

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

Related

overlay 2 point graphs on box plot in R

I'm having some issue overlaying 2 point graphs on a box plot. The code seems to work well when i added only one point graph. Here is the code below:
ggplot(data1, aes(x= reorder(DMU,order), y = Efficiency)) +
geom_boxplot() +
geom_point(data = data2, aes(x = dmu, y = eff, color = "eff")) +
scale_color_manual("", breaks = c("eff"), values = c("blue")) +
geom_point(data = data3, aes(x = DMU, y = eff2, color = "eff2")) +
scale_color_manual("", breaks = c("eff2"), values = c("red"))
I keep getting the error below:
Scale for 'colour' is already present. Adding another scale for
'colour', which will replace the existing scale.
Error: Insufficient values in manual scale. 2 needed but only 1 provided.
You cannot add scale_color_manual() twice.
Build a single dataframe for the colon:
df_points <- data.frame(x = c(data2$dmu, data3$DMU),
y = c(data2$eff, data3$eff2),
data = c("data2", "data3")
)
And then:
ggplot(data1, aes(x = reorder(DMU,order), y = Efficiency)) +
geom_boxplot() +
geom_point(data = df_points, aes(x = x, y = y, color = data)) +
scale_colour_manual(values = c("red", "blue") +
theme(legend.position = "none")
Not having the data available I could have made a mistake

Pass changed geom from object to other ggplot

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()

How to position lines at the edges of stacked bar charts

Is it possible to change the position of lines, such that they start and end at the edges of stacked bar charts instead of in the center?
R code:
library(ggplot2)
plot11 = ggplot(CombinedThickness2[CombinedThickness2$DepSequence == "Original",], aes(x = Well, y = Thickness, fill = Sequence, alpha = Visible, width = 0.3)) +
geom_bar(stat = "identity") +
scale_y_reverse()
plot11 = plot11 + geom_line(aes(group = Sequence, y = Depth, color = Sequence))
plot11
Current image:
Data:
http://pastebin.com/D7uSKBmA
It seems that what is required is segments rather than lines; that is, use geom_segment() in place of geom_line(). geom_segment requires x and y coordinates for the start and end points of the segments. Getting the end y value is a bit unwieldy. But it works with your data frame assuming that there are 30 observations for each "Well", and that the order for "Sequence" is the same for each "Well".
library(ggplot2)
df = CombinedThickness2[CombinedThickness2$DepSequence == "Original",]
# Get the y end values
index = 1:dim(df)[1]
NWell = length(unique(df$Well))
df$DepthEnd[index] = df$Depth[index + dim(df)[1]/NWell]
BarWidth = 0.3
plot11 = ggplot(df,
aes(x = Well, y = Thickness, fill = Sequence, alpha = Visible)) +
geom_bar(stat = "identity", width = BarWidth) +
scale_y_reverse() + scale_alpha(guide = "none")
plot11 = plot11 +
geom_segment(aes(x = as.numeric(Well) + 0.5*BarWidth, xend = as.numeric(Well) + (1-0.5*BarWidth),
y = Depth, yend = DepthEnd, color = Sequence))
plot11

ggplot2 - annotate means in several graph

I try to mark my graphs with the average specific of each graph :
ggplot(diamonds, aes(x = carat, fill=cut)) +
stat_density(aes(ymax = ..density.., ymin = -..density..),
geom = "ribbon", position = "identity") +
facet_grid(. ~ cut) +
xlim(0,2.5) +
geom_text(data = NULL, x = 0.6, y = 0, label = mean(carat), size=5) +
coord_flip()
For example, here I would like the graph of "Fair" is displayed average of "Fair", that of "Good" is displayed average of "Good", etc.
Also, but this is an extra, I would like to be positioned with respect to x if the average is 1.0, while the average is displayed at x = 1.0
There are a number of ways to get the labels (and the positions for the labels). Here, the dplyr package is used to summarise the diamonds data frame; that is, to obtain the required means. Also note that the labels are formatted - two decimal places. In the code below, the diamonds2 data frame contains the means and the labels, and is used in the call to geom_text.
library(ggplot2)
library(dplyr)
diamonds2 = transform(summarise(group_by(diamonds, cut), label = mean(carat)),
Label = sprintf("%.02f", label))
ggplot(diamonds, aes(x = carat, fill=cut)) +
stat_density(aes(ymax = ..density.., ymin = -..density..),
geom = "ribbon", position = "identity") +
facet_grid(. ~ cut) +
xlim(0, 2.5) +
geom_text(data = diamonds2, aes(label = Label, x = label, y = 0), size=5) +
coord_flip()

plot only a select few facets in facet_grid

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

Resources