Barplot in ggplot - r

I'm having problems making a barplot using ggplot.
I tried different combinations of qplot and gplot, but I either get a histogram, or it swaps my bars or it decides to use log-scaling.
Using the ordinary plot functions. I would do it like:
d <- 1/(10:1)
names(d) <- paste("id", 1:10)
barplot(d)

To plot a bar chart in ggplot2, you have to use geom="bar" or geom_bar. Have you tried any of the geom_bar example on the ggplot2 website?
To get your example to work, try the following:
ggplot needs a data.frame as input. So convert your input data into a data.frame.
map your data to aesthetics on the plot using `aes(x=x, y=y). This tells ggplot which columns in the data to map to which elements on the chart.
Use geom_plot to create the bar chart. In this case, you probably want to tell ggplot that the data is already summarised using stat="identity", since the default is to create a histogram.
(Note that the function barplot that you used in your example is part of base R graphics, not ggplot.)
The code:
d <- data.frame(x=1:10, y=1/(10:1))
ggplot(d, aes(x, y)) + geom_bar(stat="identity")

Related

Stacked bar plot using factor lists in base R

I am hoping to make a stacked bar plot to show two factors. The questions and answers I can find on this site that address this problem all work with data that appears to be in a matrix format and use ggplot2. My data is in lists of observations, like this:
mydata = data.frame(V1=c("A","B","B","C","C"), V2=c("X","X","Y","Z","Z"))
I would like to show categories of V1 on the x axis of my plot, but stacked to show the proportions of V2 in each bar.
I can use the "count" function in the plyr library to find the frequency of each observation,
library(plyr)
mydata.count = count(mydata)
but I don't know how to structure my barplot command to group data by the level of V1: barplot(mydata.count$freq) separates all combinations of V1 and V2 into separate bars.
If possible, I would like to create this plot using the base R barplot functions so that it is visually consistent with other plots in my study.
Here is another possibility with ggplot:
ggplot(as.data.frame(table(mydata)), aes(x=V1, y=Freq, fill=V2)) + geom_bar(stat="identity")
ggplot(as.data.frame(table(mydata)), aes(x=V2, y=Freq, fill=V1)) + geom_bar(stat="identity")

Plotting distributions of all columns in an R data frame

I'm trying to come up with a clean way to plot a grid view of all the columns in an R data frame. The problem is my dataframe has both discrete and numeric values in it. For simplicity's sake, we can use the sample dataset provided by R called iris. I would use par(mfrow(x, y)) to split my plots and maybe an mapply to cycle through each column? I'm unsure what's best here.
I'm thinking something akin to:
ggplot(iris, aes(Sepal.Length))+geom_density()
But instead plotted for each column. My concern is the "Species" column being discrete. Maybe "geom_density" wouldn't be the right plot to use here, but the idea is to see each of the data frame's variables distributions in one plot-- even the discrete ones. Bar plots for the discrete values would serve the purpose. Basically I'm trying to do the following:
Cycle through each column in the data frame
If numeric, plot a histogram
If discrete (a string basically), plot a bar plot
Any thoughts or advice would be appreciated!
You can use the function plot_grid from the cowplot package. This function takes a list of plots generated by ggplot and created a new plot, cobining them in a grid.
First, create a list of plots with lapply, using geom_density for numeric variables and geom_bar for everything else.
my_plots <- lapply(names(iris), function(var_x){
p <-
ggplot(iris) +
aes_string(var_x)
if(is.numeric(iris[[var_x]])) {
p <- p + geom_density()
} else {
p <- p + geom_bar()
}
})
Now we simply call plot_grid.
plot_grid(plotlist = my_plots)

Barplots with grouping by multiple factors

I want to make a plot that looks like this in ggplot2:
Here's a dummy dataset:
set.seed(1)
dat <- data.frame(x = exp(rnorm(6)),
f1 = factor(c("a","b","c","c","d","d")),
f2 = factor(c("","","1","2","1","2")))
And a non-working example:
ggplot(data=dat, aes(x=f1, y=x, fill= f2)) +
geom_bar(stat="identity", width=0.9)+
scale_fill_manual(values=c("red", "blue", rep("green",4)))
I could do this by brute force in base graphics, but I'm unsure how to go about it in ggplot2, and I need to use ggplot2 so as to keep this plot consistent with the theme I'm using throughout the project.
So how do I make the factors 1 and 2 go side by side? And how do I center the labels like I have in the drawing? And how do I make the colors behave?
You are trying to make a barplot with grouped bars, a question that was answered here: Grouped bar plot in ggplot
fill only determines the colors, not the grouping, this is why you get blue and green bars for c and d.

Multiple series barplot

I have matrix the looks like this (expect with four numeric variables)
GeneId<- c("x","y","z")
Var1<- c(0,1,3)
Var2<- c(1,2,1)
df<-cbind(GeneId, Var1,Var2)
What I what to plot is a bar graph where each gene has a bar for each variable grouped (i.e x would have bar1 = height 0, bar2 = 1)
I can do individual graphs by writing a loop and plotting each row:
for (i in 1:legnth(df$GeneId){
barplot(as.numeric(df[i,]), main= rownames(df)[i])
}
But I would like to have the plots on the same graph. Any ideas? I thought of doing using either ggplot2 or lattice but from what I have seen they are only able to put them in a grid together, axis are independent of each other.
The simplest answer would be to use
barplot(rbind(Var1,Var2),col=c("darkblue","red"),beside = TRUE)
I recommend you to read and experiment using barplot
Try this:
df=data.frame(GeneId=c("x","y","z"), Var1=c(0,1,3),Var2=c(1,2,1))
library(reshape2)
library(ggplot2)
df_ = melt(df, id.vars=c("GeneId"))
ggplot(df_, aes(GeneId, value, fill=variable)) +
geom_bar(stat='Identity',position=position_dodge())

How to plot graph type='h' in ggplot2

I'm wondering how to plot in ggplot2 something like this:
let's say I've got two numeric vectors:
time <-c(1,3,4,6,9,10,12), n.censor<-c(0,0,1,4,0,3,1)
and I'd like to plot:
plot(n.censor~time,type='h')
How to achieve something like this in ggplot2 ?
In this case, a "histogram" is the look you want, but the data are coded as if they're for a bar graph, since they are already aggregated. As such, your stat will be "identity".
Here's some code to use for ggplot() :
# first put your vectors into a data.frame
df <- data.frame(time, n.censor)
# plot
ggplot(df, aes(x=time, y=n.censor))+
geom_bar(stat="identity")
# or alternatively, with the histogram layer:
ggplot(df, aes(x=time, y=n.censor))+
geom_histogram(stat="identity")

Resources