How to stack a level in a bar chart? - r

I'm trying to build a stacked bar chart from a 2x2 dataframe. I'm using ggplot2 1.0.0. Unfortunately, the column with data for the B level only shows one colour, instead of two.
df <- data.frame(x1 = rep(c("A","B"), each = 2), x2 = c(75.0, 25.0, 50.0, 50.0))
fig1 <- ggplot(data = df, aes(x = x1, y = x2)) + geom_bar(aes(x1, fill = x2), stat = "identity") + xlab("") + ylab("%") + ggtitle("df")
fig1 + geom_text(aes(label = x2), vjust = -0.8, colour = "white")
The code produces the following graph: http://imgur.com/j3DEO5h
Any ideas?

You used a continous scale for fill, so two 50 values have the same colour. You could use a discrete scale like this:
ggplot(transform(df, var = as.factor(1:2)),
aes(x = x1, y = x2, fill = var)) +
geom_bar(stat = "identity")

Related

How to correctly specify a column as the fill colour in geom_ribbon?

I can't seem to be able to set different fill colours for geom_ribbon(), using one of the columns as input to fill
library(ggplot2)
time <- as.factor(c('A','B','C','D'))
grouping <- as.factor(c('GROUP1','GROUP1','GROUP1','GROUP1',
'GROUP2','GROUP2','GROUP2','GROUP2'))
x <- c(1.00,1.03,1.03,1.06,0.5,0.43,0.2,0.1)
x.upper <- x+0.05
x.lower <- x-0.05
df <- data.frame(time, x, x.upper, x.lower,grouping)
ggplot(data = df,aes(as.numeric(time),x,group=grouping,color=grouping)) +
geom_ribbon(data = df, aes(x=as.numeric(time), ymax=x.upper, ymin=x.lower),
fill=grouping, alpha=.5) +
geom_point() + labs(title="My ribbon plot",x="Time",y="Value") +
scale_x_continuous(breaks = 1:4, labels = levels(df$time))
I get the error Error: Unknown colour name: grouping but fill=c("pink","blue") works fine. I don't want to specify the colours manually.
All other examples I can find simply list the column in the fill argument so I'm not sure what I'm doing incorrectly.
Move fill = grouping inside aes so that this column is mapped to the fill variable.
ggplot(data = df, aes(as.numeric(time), x, color = grouping)) +
geom_ribbon(data = df, aes(ymax = x.upper, ymin = x.lower,
fill = grouping), alpha = 0.5) +
geom_point() +
labs(title = "My ribbon plot", x = "Time", y = "Value") +
scale_x_continuous(breaks = 1:4, labels = levels(df$time))

R ggplot2 with stacked column instead of grouped

I want to plot the data shown below as a grouped bar_plot.
I tried position = "dodge" or position = "dodge2" but it didn't work. Ι also tried position = position_dodge()
It kinda works if i use geom_bar instead of geom_col and remove the y=overlap_percent:
p3 <- ggplot(data = comp_coors, aes(x = species_code, fill = mirna_form)) +
geom_bar(position = "dodge2") + theme_classic()
p3
but i would like the y_axis to have the overlap_percent.
Another attempt which ends in a stacked barplot is:
p2 <- ggplot(data = comp_coors, aes(x = species_code, y = overlap_percent, fill = mirna_form)) +
geom_bar(stat = "identity") + theme_classic()
p2
Finally by using geom_col, it returns this which it doesn't make sense, at least to me:
p4 <- ggplot(data = comp_coors, aes(x = species_code, y = overlap_percent, fill = mirna_form)) +
geom_col(position = "dodge") + theme_classic()
p4
The data that i want to plot :
comp_coors <- data.table( species = c("aae","cel", "dme","hsa", "mdo"),
mirna_form = c("mature", "precursor"),
overlap_percent = c(100.0, 100.0, 88.0, 95.5, 91.7, 100.0, 96.6, 98.4),
overlapping_attribute = c("ID=MIMAT0014285;Alias=MIMAT0014285", "ID=MI0000043;Alias=MI0000043;Name=cel-mir-72", "ID=MIMAT0000401;Alias=MIMAT0000401;Name=dme-miR-", "ID=MI0000791;Alias=MI0000791;Name=hsa-mir-383", "ID=MI0005331;Alias=MI0005331;Name=mdo-let-7g")
)
Try using species as a factor and add stat = "identity" like this:
ggplot(data = comp_coors, aes(x = factor(species), y = overlap_percent, fill = mirna_form)) +
geom_bar(position = "dodge", stat = "identity") + theme_classic() + labs(x = "Species", y = "Overlap percent")
Output:
A grouped barplot with overlap_percent on y-axis right.

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

How to add line to point shapes in ggplot2 legend

I want to create a black and white plot using ggplot2, where the data is plotted by category using a combination of lines and points. However, the legend only shows the point shape, with no line running through it, unless I add color to the plot.
Here is some example data to illustrate the problem with:
## Create example data
set.seed(123)
dat <- data.frame(
time_period = rep(1:4, each = 3),
category = rep(LETTERS[1:3], 4),
y = rnorm(12)
)
Here is an example of a color plot, so you can see how I want the legend to look:
library(ggplot2)
## Generate plot with color
ggplot(data = dat, mapping = aes(x = time_period, y = y, color = category)) +
geom_line(aes(group = category)) +
geom_point(aes(shape = category), size = 2) +
theme_bw()
However, if I move to grayscale (which I need to be able to do), the line running through the point in the legend disappears, which I'd like to avoid:
## Generate plot without color
ggplot(data = dat, mapping = aes(x = time_period, y = y)) +
geom_line(aes(group = category)) +
geom_point(aes(shape = category), size = 2) +
theme_bw()
How can I add a line through the point symbols in the legend with a grayscale plot?
I would suggest this approach:
#Plot
ggplot(data = dat, mapping = aes(x = time_period, y = y,group = category,shape = category)) +
geom_line(color='gray',show.legend = T) +
geom_point(size = 2) +
theme_bw()
Output:

geom_bar ggplot2 subtract two Values

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

Resources