Grouped Barplot with three measures - r

I am trying to reproduce the following graph in R which is generated via Excel.
The CSV file has following content:
> myd
type am tae tac
1 kA 81.73212 73.07151 26.92849
2 kI 78.87324 84.50704 15.49296
3 kL 82.52184 69.91262 30.08738
4 kS 82.67147 69.31411 30.68589
5 sA 81.67176 73.31295 26.68705
6 sI 79.54135 81.83460 18.16540
7 sL 81.58485 73.66061 26.33939
8 sS 82.09616 71.61536 28.38464
The following R code creates am on the y axis, but I also want to add tae and tac.
ggplot(myd, aes(x=type, y=am)) + geom_bar(stat="identity")
Do you know how I can add this in R to have it like in the Excel diagram?

require(reshape2)
myd_molten <- melt(data = myd, id.vars = "type")
require(ggplot2)
ggplot(myd_molten, aes(x = type, y = value, fill=variable)) +
geom_bar(stat="identity", position=position_dodge())+
coord_flip()

Try
library(tidyr)
library(dplyr)
library(ggplot2)
gather(myd, Var, Val, am:tac) %>%
ggplot(., aes(x=type, y=Val))+
geom_bar(aes(fill=Var), position='dodge', stat='identity')+
coord_flip()

Related

ggplot bar plot side by side using two variables [duplicate]

This question already has an answer here:
How to create side-by-side bar charts (for multiple series) with ggplot?
(1 answer)
Closed 5 years ago.
I want to create a barplot using ggplot in R studio using two variables side by side. I tried following other people suggestions I found online, but I cant get it to work.
Here's the data I'm using:
x <- c(5,17,31,9,17,10,30,28,16,29,14,34)
y <- c(1,2,3,4,5,6,7,8,9,10,11,12)
day <- c(1,2,3,4,5,6,7,8,9,10,11,12)
So, what I'm trying to do is have days on the x-axis and side by side barplots of x and y (with x & y being colored) corresponding to the day number.
First thing i did was make a data frame :
df1 <- data.frame(x,y,day)
and then I tried:
ggplot(df1, aes(x = day, y = x,y)) + geom_bar(stat = "identity",color = x, width = 1, position="dodge")
But I just can't get it to work properly. Any suggestions as to how I'd achieve this?
You have the right idea, I think the melt() function from the reshape2 package is what you're looking for.
library(ggplot2)
library(reshape2)
x <- c(5,17,31,9,17,10,30,28,16,29,14,34)
y <- c(1,2,3,4,5,6,7,8,9,10,11,12)
day <- c(1,2,3,4,5,6,7,8,9,10,11,12)
df1 <- data.frame(x, y, day)
df2 <- melt(df1, id.vars='day')
head(df2)
ggplot(df2, aes(x=day, y=value, fill=variable)) +
geom_bar(stat='identity', position='dodge')
EDIT
I think the pivot_longer() function from the tidyverse tidyr package might now be the better way to handle these types of data manipulations. It gives quite a bit more control than melt() and there's also a pivot_wider() function as well to do the opposite.
library(ggplot2)
library(tidyr)
x <- c(5,17,31,9,17,10,30,28,16,29,14,34)
y <- c(1,2,3,4,5,6,7,8,9,10,11,12)
day <- c(1,2,3,4,5,6,7,8,9,10,11,12)
df1 <- data.frame(x, y, day)
df2 <- tidyr::pivot_longer(df1, cols=c('x', 'y'), names_to='variable',
values_to="value")
head(df2)
ggplot(df2, aes(x=day, y=value, fill=variable)) +
geom_bar(stat='identity', position='dodge')
Or you could use facet_wrap to produce two plots:
library("ggplot2")
library("reshape")
x <- c(5,17,31,9,17,10,30,28,16,29,14,34)
y <- c(1,2,3,4,5,6,7,8,9,10,11,12)
day <- c(1,2,3,4,5,6,7,8,9,10,11,12)
df1 <- data.frame(x,y,day)
df2 <- reshape::melt(df1, id = c("day"))
ggplot(data = df2, aes(x = day, y = value, fill = variable)) + geom_bar(stat = "identity")+ facet_wrap(~ variable) + scale_x_continuous(breaks=seq(1,12,2))
If you want the bars with color according to the day use fill = day:
ggplot(data = df2, aes(x = day, y = value, fill = day)) + geom_bar(stat = "identity") + facet_wrap(~ variable) + scale_x_continuous(breaks=seq(1,12,2))

Grouped bins with multiple y axis

I have a data frame with five columns and five rows. the data frame looks like this:
df <- data.frame(
day=c("m","t","w","t","f"),
V1=c(5,10,20,15,20),
V2=c(0.1,0.2,0.6,0.5,0.8),
V3=c(120,100,110,120,100),
V4=c(1,10,6,8,8)
)
I want to do some plots so I used the ggplot and in particular the geom_bar:
ggplot(df, aes(x = day, y = V1, group = 1)) + ylim(0,20)+ geom_bar(stat = "identity")
ggplot(df, aes(x = day, y = V2, group = 1)) + ylim(0,1)+ geom_bar(stat = "identity")
ggplot(df, aes(x = day, y = V3, group = 1)) + ylim(50,200)+ geom_bar(stat = "identity")
ggplot(df, aes(x = day, y = V4, group = 1)) + ylim(0,15)+ geom_bar(stat = "identity")
My question is, How can I do a grouped ggplot with geom_bar with multiple y axis? I want at the x axis the day and for each day I want to plot four bins V1,V2,V3,V4 but with different range and color. Is that possible?
EDIT
I want the y axis to look like this:
require(reshape)
data.m <- melt(df, id.vars='day')
ggplot(data.m, aes(day, value)) +
geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
facet_grid(variable ~ .)
You can also change the y-axis limits if you like (here's an example).
Alternately you may have meant grouped like this:
require(reshape)
data.m <- melt(df, id.vars='day')
ggplot(data.m, aes(day, value)) +
geom_bar(aes(fill = variable), position = "dodge", stat="identity")
For the latter examples if you want 2 Y axes then you just create the plot twice (once with a left y axis and once with a right y axis) then use this function:
double_axis_graph <- function(graf1,graf2){
graf1 <- graf1
graf2 <- graf2
gtable1 <- ggplot_gtable(ggplot_build(graf1))
gtable2 <- ggplot_gtable(ggplot_build(graf2))
par <- c(subset(gtable1[['layout']], name=='panel', select=t:r))
graf <- gtable_add_grob(gtable1, gtable2[['grobs']][[which(gtable2[['layout']][['name']]=='panel')]],
par['t'],par['l'],par['b'],par['r'])
ia <- which(gtable2[['layout']][['name']]=='axis-l')
ga <- gtable2[['grobs']][[ia]]
ax <- ga[['children']][[2]]
ax[['widths']] <- rev(ax[['widths']])
ax[['grobs']] <- rev(ax[['grobs']])
ax[['grobs']][[1]][['x']] <- ax[['grobs']][[1]][['x']] - unit(1,'npc') + unit(0.15,'cm')
graf <- gtable_add_cols(graf, gtable2[['widths']][gtable2[['layout']][ia, ][['l']]], length(graf[['widths']])-1)
graf <- gtable_add_grob(graf, ax, par['t'], length(graf[['widths']])-1, par['b'])
return(graf)
}
I believe there's also a package or convenience function that does the same thing.
First I reshaped as described in the documentation in the link below the question.
In general ggplot does not support multiple y-axis. I think it is a philosophical thing. But maybe faceting will work for you.
df <- read.table(text = "day V1 V2 V3 V4
m 5 0.1 120 1
t 10 0.2 100 10
w 2 0.6 110 6
t 15 0.5 120 8
f 20 0.8 100 8", header = TRUE)
library(reshape2)
df <- melt(df, id.vars = 'day')
ggplot(df, aes(x = variable, y = value, fill = variable)) + geom_bar(stat = "identity") + facet_grid(.~day)
If I understand correctly you want to include facets in your plot. You have to use reshape2 to get the data in the right format. Here's an example with your data:
df <- data.frame(
day=c("m","t","w","t","f"),
V1=c(5,10,20,15,20),
V2=c(0.1,0.2,0.6,0.5,0.8),
V3=c(120,100,110,120,100),
V4=c(1,10,6,8,8)
)
library(reshape2)
df <- melt(df, "day")
Then plot with and include facet_grid argument:
ggplot(df, aes(x=day, y=value)) + geom_bar(stat="identity", aes(fill=variable)) +
facet_grid(variable ~ .)

Scatter plot with ggplot

I want to do a scatter (xy) plot of variables in a melted data frame as shown below.
df
class var mean
0 x 4.25
0 y 6.25
1 x 2.00
1 y 11.00
I have tried this, but it plots 4 points. How can plot x and y?
library(ggplot2)
ggplot(df, aes(x=mean, y=mean, group=var, colour=class)) +
geom_point( size=5, shape=21, fill="white")
As Heroka pointed out, you need the data to be in a more wide type format. If the data was read in like this, you may use the following to convert it.
## you don't need this since you already have df
text = "class var mean
0 x 4.25
0 y 6.25
1 x 2.00
1 y 11.00"
df = read.delim(textConnection(text),header=TRUE,strip.white=TRUE,
stringsAsFactors = FALSE, sep = " ");df2
## use this library to switch from long-wide
library(reshape2)
df2 = dcast(df, class ~ var, value.var = "mean")
library(ggplot2)
ggplot(df2, aes(x=x, y=y, colour=class)) +
geom_point( size=5, shape=21, fill="white")

ggplot not adding legend. What am I missing? very new to R

I'm plotting three samples with ggplot but it's not adding a legend for the samples. It's not spitting out any error message so I'm not sure where I'm going wrong. I'd really appreicate some guidance.
I've tried to declare color for each sample for the legend manually but there is still no legend on the plot.
df<-data.frame(samples$V1, samples$V2, samples$V3, samples$V4, samples$V5, samples$V6, samples$V7)
CG_methplot <- ggplot(df, aes(x=samples$V1,))+
scale_x_continuous(breaks=number_ticks(10))+
xlab("bins")+
ylab("mean CG methylation")+
geom_point(aes(y=samples$V2), size=3, colour='#009933')+
geom_point(aes(y=samples$V3), size=3, colour='#FF0000')+
geom_point(aes(y=samples$V4), size=3, colour='#0033FF')+
scale_color_manual(values=c("samples1"="009933", "sample2"="FF0000", "sample3" ="0033FF"))
CG_methplot
As requested, sample data.
head(df)
samples.V1 samples.V2 samples.V3 samples.V4 samples.V5 samples.V6 samples.V7
1 1 0.033636 0.027857 0.028830 0.029836 0.024457 0.024930
2 2 0.032094 0.029620 0.028005 0.028294 0.026220 0.024105
3 3 0.032011 0.027212 0.029728 0.028211 0.023812 0.025828
4 4 0.030857 0.029833 0.028907 0.027057 0.026433 0.025007
5 5 0.028480 0.028080 0.028553 0.024680 0.024680 0.024653
6 6 0.029445 0.027099 0.029346 0.025645 0.023699 0.025446
library(reshape2)
melted <- melt(df, id.vars = "V1")
p <- ggplot(melted, aes(x = V1, y = value, colour = variable))
p + geom_point()

Clustered Bar plot in r using ggplot2

Snip of my data frame is
Basically i want to display barplot which is grouped by Country i.e i want to display no of people doing suicides for all of the country in clustered plot and similarly for accidents and Stabbing as well.I am using ggplot2 for this.I have no idea how to do this.
Any helps.
Thanks in advance
Edit to update for newer (2017) package versions
library(tidyr)
library(ggplot2)
dat.g <- gather(dat, type, value, -country)
ggplot(dat.g, aes(type, value)) +
geom_bar(aes(fill = country), stat = "identity", position = "dodge")
Original Answer
dat <- data.frame(country=c('USA','Brazil','Ghana','England','Australia'), Stabbing=c(15,10,9,6,7), Accidents=c(20,25,21,28,15), Suicide=c(3,10,7,8,6))
dat.m <- melt(dat, id.vars='country')
I guess this is the format you're after?
ggplot(dat.m, aes(variable, value)) +
geom_bar(aes(fill = country), position = "dodge")
library(ggplot2)
library(reshape2)
df <- data.frame(country=c('USA','Brazil','Ghana','England','Australia'), Stabbing=c(15,10,9,6,7), Accidents=c(20,25,21,28,15), Suicide=c(3,10,7,8,6))
mm <- melt(df, id.vars='country')
ggplot(mm, aes(x=country, y=value)) + geom_bar(stat='identity') + facet_grid(.~variable) + coord_flip() + labs(x='',y='')

Resources