I'm trying to create piechart similar to radial plot (plotrix), in ggplot2.
Basically the slices would have different length.
radii <- c(2,3,2,1,3,1,2,3,2)
color <- c("lightgrey", "chartreuse", "lightgrey", "darkturquoise", "darkolivegreen3",
"orangered", "lightgrey", "darkseagreen1", "lightgrey")
radial.pie(radii, labels = NA, sector.colors = color,
show.grid = F, show.grid.labels = F ,show.radial.grid = T,
radial.labels = F, clockwise = T,start=3)
Is there an easy way to do this? The reason for doing it in ggplot is that I want to have a this piechart on top of a ggplot violin plot in one page using plot_grid.
This answer was copied from:
Making polar plots with ggplot2
by Carolyn Parkinson
(April 10, 2015)
http://rstudio-pubs-static.s3.amazonaws.com/72298_c1ba7f77276a4f27a0f375cadc9fac5d.html
Basically, all you have to do is plot a bar plot with coord_ploar() to make it this kind of radial plot:
require(ggplot2)
# function to compute standard error of mean
se <- function(x) sqrt(var(x)/length(x))
set.seed(9876)
DF <- data.frame(variable = as.factor(1:10),
value = sample(10, replace = TRUE))
ggplot(DF, aes(variable, value, fill = variable)) +
geom_bar(width = 1, stat = "identity", color = "white") +
geom_errorbar(aes(ymin = value - se(DF$value),
ymax = value + se(DF$value),
color = variable),
width = .2) +
scale_y_continuous(breaks = 0:nlevels(DF$variable)) +
theme_gray() +
theme(axis.ticks = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.line = element_blank()) +
coord_polar()
Related
I want to plot the result of a paried t test with a plot containing points, lines and bars, like the plot on the paper (Bellmund, at al. Mapping sequence structure in the human lateral entorhinal cortex. Elife, 8. doi:10.7554/eLife.45333).
See the screenshot of the plot:
In my practise, it is easy to put geom_point, geom_line and geom_bar in the same plot, but the problem comes up when I try to change the positions of the points and lines while remain the bars unchanged.
I tried to use position_dodge() in point plot, however, it would move all the elements in the plot. Any ideas? Thanks for help!
library(ggplot2)
id <- rep(c(1:29), times = 2)
condition <- c(rep('vector.angle', times = 29), rep('baseline', times = 29))
value <- rnorm(58, mean = 22, sd = 27)
v.angle.gg <- data.frame(id, condition, value)
ggplot(data = v.angle.gg, aes(x = condition, y = value)) +
geom_hline(aes(yintercept = 0), color = "black", size = 1.5, linetype = 14) +
stat_summary(fun.y = mean, geom = "bar", aes(x = condition, y = value, fill = condition),
width = 0.3, position = position_identity()) +
geom_line(aes(group= id), size = 1, color = 'gray') +
geom_point(size = 4, position = position_dodge(width= 1)) +
scale_fill_brewer(palette="Paired") +
xlab('Conditions') + ylab('Parameter Estimations') +
theme(
title = element_text(size=rel(1.3),face="bold", colour = "black"),
axis.line.x = element_line(size = 1, colour = "black"),
axis.text.x=element_text(size=rel(1.3),face="bold", colour = "black"),
axis.ticks.x = element_line(size=rel(2), colour = "black"),
axis.line.y = element_line(size = 1, colour = "black"),
axis.text.y=element_text(size=rel(1.3),face="bold", colour = "black"),
axis.ticks.y = element_line(size=rel(2), colour = "black"),
panel.border = element_blank(),
panel.background = element_blank(),
panel.grid = element_blank()
) +
guides(fill=F,color=F)
The question asks for ideas, here is a hack.
Bar plots plot the bars at consecutive integers x, in this case c(1, 2). So you need the points and the lines connecting them at c(1.2, 1.8). Now, in the data set column condition has the alphabetically larger values first and they will be coerced to factor before plotting. So the new x coordinates vector will be subset with condition correctly. Not clear?
f <- v.angle.gg$condition
f <- c(1.2, 1.8)[as.integer(factor(f))]
These are the new coordinates. And they must be set in both geom_line and geom_point. Makes more sense now? I hope so.
I have simplified the graph so that only the relevant part is plotted.
ggplot(data = v.angle.gg, aes(x = condition, y = value)) +
geom_hline(aes(yintercept = 0), color = "black", size = 1.5, linetype = 14) +
stat_summary(fun = mean, geom = "bar", aes(x = condition, y = value, fill = condition),
width = 0.3, position = position_identity()) +
geom_line(aes(x = f, group= id), size = 1, color = 'gray') +
geom_point(aes(x = f), size = 4, position = position_dodge(width= 1))
I'm using ggplot2 in R to create maps. In the past, I have been able to successfully use the scale_fill_gradient() function to control geom_point fills. However, when I run the code below (with example table provided), I simply get a map of all black points. The legend appears correct, but the points never change color. I think my desired variable is not mapping to the fill aesthetic, but I cannot figure out why. Thank you in advance!
(if it matters, I am using tibble package to define tables)
table = tibble(long = c(15.28, 15.29, 15.3, 15.31, 15.32), lat = c(-4.4, -4.39, -4.38, -4.37, -4.36), consumption = c(NA, 3, 54, 6, 8))
mapping = aes_string(x = 'long', y = 'lat', fill = 'consumption')
# define breaks, limits, colors
low = 'seashell'
high = 'tan3'
breaks = c(0, max(na.omit(table)[['consumption']]))
limits = breaks
# plot
p <- ggplot() +
# points
geom_point(mapping = mapping, data = table, alpha = 0.7, size = 4) +
# point colors
scale_fill_gradient(low = low, high = high, na.value = 'darkgrey', guide = 'colorbar', aesthetics = 'fill'
, breaks = breaks, limits = limits) +
# title
ggtitle('consumption') +
# title formatting
theme(plot.title = element_text(color = "red", size = 10, face = "bold", hjust=0),
legend.position="bottom",
legend.text=element_text(size=9),
legend.title=element_text(size=9)) +
# legend
guides(fill=guide_colorbar(title='consumption')) +
# get rid of axes, etc.
theme(axis.line = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank()) +
xlab('') +
ylab('') +
# make legend correct
theme(legend.box = 'vertical') +
# add max/min corresponding to map
xlim(c(15.28, 15.38)) +
ylim(c(-4.41, -4.30))
As mentioned in the comments you have to change the fill to color. Here is how I achieved it:
library(tidyverse)
table = tibble(long = c(15.28, 15.29, 15.3, 15.31, 15.32), lat = c(-4.4, -4.39, -4.38, -4.37, -4.36), consumption = c(NA, 3, 54, 6, 8))
##Changed here to color
mapping = aes_string(x = 'long', y = 'lat', color = 'consumption')
# define breaks, limits, colors
low = 'seashell'
high = 'tan3'
breaks = c(0, max(na.omit(table)[['consumption']]))
limits = breaks
# plot
ggplot() +
# points
geom_point(mapping = mapping,
data = table, alpha = 0.7, size = 4) +
# point colors
#Change here to aesthetics = color
scale_color_gradient(low = low, high = high, na.value = 'darkgrey', guide = 'colorbar', aesthetics = 'color'
, breaks = breaks, limits = limits) +
# title
ggtitle('consumption') +
# title formatting
theme(plot.title = element_text(color = "red", size = 10, face = "bold", hjust=0),
legend.position="bottom",
legend.text=element_text(size=9),
legend.title=element_text(size=9)) +
# legend
guides(fill=guide_colorbar(title='consumption')) +
# get rid of axes, etc.
theme(axis.line = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank()) +
xlab('') +
ylab('') +
# make legend correct
theme(legend.box = 'vertical') +
# add max/min corresponding to map
xlim(c(15.28, 15.38)) +
ylim(c(-4.41, -4.30))
I have something strange in my plot which I can't figure out. It seems like the transparency of the bubbles in my plot are different to the transparency of those in the legend.
I've found it challenging to provide a reprex for this, so I'm hoping just the code is okay:
ggplot() + geom_polygon(data = all.shp, aes(x = long, y = lat, group=group),
color = "grey", size = 0.3,fill = NA) +
geom_point(data=substation_results, aes(x=Easting, y=Northing, size=factor(Population),
colour=factor(Population), fill = NA), alpha=0.5) +
coord_equal() +
scale_fill_brewer(palette="Spectral", name =
expression('Voltage\nInstability\nZones')) +
theme(axis.text = element_blank(), axis.ticks = element_blank(), axis.title = element_blank(),
legend.position = "right") +
guides(fill = guide_legend(reverse = FALSE)) +
labs(title = "(A) Population Served\nPer Substation") +
scale_colour_manual(name = "Population\nServed Per\nSubstation\n(Million)",
labels = c("<0.15", "0.15-0.3", "0.3-0.45", "0.45-0.6",
"0.6-0.75", "0.75-0.9",">0.9"),
values = c("skyblue4", "blue", "green", "yellow", "orange", "red", "brown")) +
scale_size_manual(name = "Population\nServed Per\nSubstation\n(Million)",
labels = c("<0.15", "0.15-0.3", "0.3-0.45", "0.45-0.6",
"0.6-0.75", "0.75-0.9",">0.9"),
values = c(1, 2, 3, 4, 5, 6, 7))
This is the image of the plot. You can see the colour of the bubbles and the legend are completely different. Importantly, changing the alpha in geom_point does not change the difference:
I resolved the issue by removing duplicate observations using:
transformer_node_count <- unique(transformer_node_count)
and achieving the desired output:
Is it possible to draw a heatmap with circles instead of square in ggplot2? It would be neat to not only represent the values by a color gradient but also by the circle size.
I am thinking of a graph like this dot heatmap where also the circle sizes are alternated by their specific value. I already read myself into heatmapping with ggplot2 but couldn't find a solution. For heatmapping I alternated the example posted on learnr.wordpress.com to:
library(ggplot2)
library(plyr)
library(reshape2)
library(scales)
kreuz <- read.csv("http://datasets.flowingdata.com/ppg2008.csv")
kreuz.m <- melt(kreuz)
(p <- ggplot(kreuz.m, aes(Name, variable)) +
geom_tile(aes(fill = value), colour = "white") +
scale_fill_gradient2(breaks=waiver(), name="binding strength",
low ="white", mid= ("lightblue"), high = "steelblue", midpoint = 4))
base_size <- 10
p + theme_grey(base_size = base_size) +
theme(panel.grid.major = element_blank())+
labs(x = "Patient ID", y = "Phage Motives", title = "Cross Reactivity")+
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0)) +
theme(legend.position = "right", axis.ticks = element_blank(),
axis.text.x = element_text(size = base_size *0.8, angle = 270, hjust = 0,
colour = "grey50"))+
labs(x = "Patient ID", y = "Phagemotives", title = "cross reactivity")
I would be very greatful for some hints!
In this example size and colour both correspond with the variable value because it's the only variable numeric available in the kreuz.m dataset.
ggplot(kreuz.m, aes(Name, variable)) +
geom_point(aes(size = value, colour=value))
Is it possible to draw a heatmap with circles instead of square in ggplot2? It would be neat to not only represent the values by a color gradient but also by the circle size.
I am thinking of a graph like this dot heatmap where also the circle sizes are alternated by their specific value. I already read myself into heatmapping with ggplot2 but couldn't find a solution. For heatmapping I alternated the example posted on learnr.wordpress.com to:
library(ggplot2)
library(plyr)
library(reshape2)
library(scales)
kreuz <- read.csv("http://datasets.flowingdata.com/ppg2008.csv")
kreuz.m <- melt(kreuz)
(p <- ggplot(kreuz.m, aes(Name, variable)) +
geom_tile(aes(fill = value), colour = "white") +
scale_fill_gradient2(breaks=waiver(), name="binding strength",
low ="white", mid= ("lightblue"), high = "steelblue", midpoint = 4))
base_size <- 10
p + theme_grey(base_size = base_size) +
theme(panel.grid.major = element_blank())+
labs(x = "Patient ID", y = "Phage Motives", title = "Cross Reactivity")+
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0)) +
theme(legend.position = "right", axis.ticks = element_blank(),
axis.text.x = element_text(size = base_size *0.8, angle = 270, hjust = 0,
colour = "grey50"))+
labs(x = "Patient ID", y = "Phagemotives", title = "cross reactivity")
I would be very greatful for some hints!
In this example size and colour both correspond with the variable value because it's the only variable numeric available in the kreuz.m dataset.
ggplot(kreuz.m, aes(Name, variable)) +
geom_point(aes(size = value, colour=value))