Histogram
We can use one of our previous objects to create a histogram. You can get it back with:
rnaseq2 <- read_csv("DataViz_source_files-main/files/GSE150029_rnaseq_log2_long.csv")
ggplot(rnaseq2, aes(x=log2_counts)) +
geom_histogram()
We can split the histogram per sample:
ggplot(rnaseq2, aes(x=log2_counts, color=sample)) +
geom_histogram()
Set position to “identity”, so histograms will not be on top of each other:
ggplot(rnaseq2, aes(x=log2_counts, color=sample)) +
geom_histogram(position="identity")
Set alpha (transparency to 0.2):
ggplot(rnaseq2, aes(x=log2_counts, color=sample)) +
geom_histogram(position="identity", alpha=0.2)
Express as density instead of counts:
ggplot(rnaseq2, aes(x=log2_counts, color=sample)) +
geom_histogram(aes(y=after_stat(density)), position="identity", alpha=0.2) +
theme_classic()
Density plot
The structure is pretty much the same:
ggplot(rnaseq2, aes(x=log2_counts, color=sample)) +
geom_density(alpha=0.2) +
theme_classic()
You can use fill instead:
ggplot(rnaseq2, aes(x=log2_counts, fill=sample)) +
geom_density(alpha=0.2) +
theme_classic()
Histogram + density
As we combined geom_boxplot and geom_violin in a previous exercise, we can also combine geom_histogram and geom_density:
ggplot(rnaseq2, aes(x=log2_counts, color=sample)) +
geom_histogram(aes(y=after_stat(density)), position="identity", alpha=0.2) +
geom_density(alpha=0.2) +
theme_classic()