I have data of the form:
Day A B
1 1 4
1 2 5
1 3 6
2 2 2
2 3 4
2 5 6
3 6 7
3 4 6
And I would like to display this on a single chart, with Day along the x-axis, and with each x-position having a boxplot for each of A and B (colour coded).
Here's a (slight) modification of an example form the ?boxplot help page. The examples show off many common uses of the functions.
tg <- data.frame(
dose=ToothGrowth$dose[1:30],
A=ToothGrowth$len[1:30],
B=ToothGrowth$len[31:60]
)
head(tg)
# dose A B
# 1 0.5 4.2 15.2
# 2 0.5 11.5 21.5
# 3 0.5 7.3 17.6
# 4 0.5 5.8 9.7
# 5 0.5 6.4 14.5
# 6 0.5 10.0 10.0
boxplot(A ~ dose, data = tg,
boxwex = 0.25, at = 1:3 - 0.2,
col = "yellow",
main = "Guinea Pigs' Tooth Growth",
xlab = "Vitamin C dose mg",
ylab = "tooth length",
xlim = c(0.5, 3.5), ylim = c(0, 35), yaxs = "i")
boxplot(B ~ dose, data = tg, add = TRUE,
boxwex = 0.25, at = 1:3 + 0.2,
col = "orange")
legend(2, 9, c("A", "B"),
fill = c("yellow", "orange"))
Try:
ddf = structure(list(Day = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L), A = c(1L,
2L, 3L, 2L, 3L, 5L, 6L, 4L), B = c(4L, 5L, 6L, 2L, 4L, 6L, 7L,
6L)), .Names = c("Day", "A", "B"), class = "data.frame", row.names = c(NA,
-8L))
mm = melt(ddf, id='Day')
ggplot(mm)+geom_boxplot(aes(x=factor(Day), y=value, fill=variable))
Related
I need help in order to add colors to ggplot objects (specificaly geom_bar).
Here is my data
Names Family Groups Values
H.sapiens A G1 2
H.erectus A G1 6
H.erectus B G2 12
M.griseus C G2 3
A.mellifera D G3 3
L.niger D G3 8
H.erectus D G3 2
L.niger A G1 3
L.niger B G2 3
A.mellifera A G1 8
And so far I suceeded to create this plot :
with this code :
library(ggplot2)
library(ggstance)
library(ggthemes)
ggplot(table, aes(fill=Family, y=Names, x=Values)) +
geom_barh(stat="identity",colour="white")+ theme_minimal() +
scale_x_continuous(limits = c(0,60), expand = c(0, 0))
and now I would like to change the color depending of Groups. More precisely I would like to choose a major color for each group, for instance: G1= blue ; G2 = Green ; G3= Red.
and for each Family to get a gradient within these colors. For instance, B will be darkblue and C ligthblue.
Does someone have an idea, please ?
Here are the data :
dput(table)
structure(list(Names = structure(c(3L, 2L, 2L, 5L, 1L, 4L, 2L,
4L, 4L, 1L), .Label = c("A.mellifera", "H.erectus", "H.sapiens",
"L.niger", "M.griseus"), class = "factor"), Family = structure(c(1L,
1L, 2L, 3L, 4L, 4L, 4L, 1L, 2L, 1L), .Label = c("A", "B", "C",
"D"), class = "factor"), Groups = structure(c(1L, 1L, 2L, 2L,
3L, 3L, 3L, 1L, 2L, 1L), .Label = c("G1", "G2", "G3"), class = "factor"),
Values = c(2L, 6L, 12L, 3L, 3L, 8L, 2L, 3L, 3L, 8L)), class = "data.frame", row.names = c(NA,
-10L))
You may perhaps tweak this one to suit your requirements (I have changed your sample data a bit to show you different gradient among same Group)
df <- read.table(header = T, text = "Names Family Groups Values
H.sapiens A G1 2
H.erectus B G1 6
H.erectus B G2 12
M.griseus C G2 3
A.mellifera D G3 3
L.niger D G3 8
H.erectus A G3 2
L.niger A G1 3
L.niger B G2 3
A.mellifera C G1 8")
library(tidyverse)
df %>% ggplot() +
geom_col(aes(x = Names, y = Values, fill = Groups, alpha = as.integer(as.factor(Family)))) +
coord_flip() +
scale_fill_manual(name = "Groups", values = c("blue", "green", 'red')) +
scale_alpha_continuous(name = "Family", range = c(0.2, 0.7)) +
theme_classic()
Created on 2021-06-12 by the reprex package (v2.0.0)
We can create range of colours for each Group then match on order of Family. You might need to play around with colours to make the difference more prominent:
cols <- lapply(list(G1 = c("darkblue", "lightblue"),
G2 = c("darkgreen", "lightgreen"),
G3 = c("red4", "red")),
function(i) colorRampPalette(i)(length(unique(table$Family))))
table$col <- mapply(function(g, i) cols[[ g ]][ i ],
g = table$Groups, i = as.numeric(table$Family))
ggplot(table, aes(x = Values, y = Names, fill = col )) +
geom_barh(stat = "identity", colour = "white") +
scale_x_continuous(limits = c(0, 60), expand = c(0, 0)) +
scale_fill_identity() +
theme_minimal()
This question already has answers here:
Customize axis labels
(4 answers)
How to use Greek symbols in ggplot2?
(4 answers)
Closed 2 years ago.
Hello everyone and happy new year !!! I would need help in order to improve a ggplot figure.
I have a dfataframe (df1) that looks like so:
x y z
1 a 1 -0.13031299
2 b 1 0.71407346
3 c 1 -0.15669153
4 d 1 0.39894708
5 a 2 0.64465669
6 b 2 -1.18694632
7 c 2 -0.25720456
8 d 2 1.34927378
9 a 3 -1.03584455
10 b 3 0.14840876
11 c 3 0.50119220
12 d 3 0.51168810
13 a 4 -0.94795328
14 b 4 0.08610489
15 c 4 1.55144239
16 d 4 0.20220334
Here is the data as dput() and my code:
df1 <- structure(list(x = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("a", "b", "c", "d"
), class = "factor"), y = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L), z = c(-0.130312994048691, 0.714073455094197,
-0.156691533710652, 0.39894708481517, 0.644656691110372, -1.18694632145378,
-0.257204564112021, 1.34927378214664, -1.03584454605617, 0.148408762003154,
0.501192202628166, 0.511688097742773, -0.947953281835912, 0.0861048893885463,
1.55144239199118, 0.20220333664676)), class = "data.frame", row.names = c(NA,
-16L))
library(ggplot2)
df1$facet <- ifelse(df1$x %in% c("c", "d"), "cd", df1$x)
p1 <- ggplot(df1, aes(x = x, y = y))
p1 <- p1 + geom_tile(aes(fill = z), colour = "grey20")
p1 <- p1 + scale_fill_gradient2(
low = "darkgreen",
mid = "white",
high = "darkred",
breaks = c(min(df1$z), max(df1$z)),
labels = c("Low", "High")
)
p1 + facet_grid(.~facet, space = "free", scales = "free_x") +
theme(strip.text.x = element_blank())
With this code (inspired from here) I get this figure:
But I wondered if someone had an idea in order to :
Add Greek letter in the x axis text (here alpha and beta)
To add sub Y axis element (here noted as Element 1-3) where Element1 (from 0 to 1); Element2 (from 1 to 3) and Element3 (from 3 to the end)
the result should be something like:
I am trying to create a single bar or rectangle plot in R with colors based on some groupings and order based on some values. See example below:
For those interested in more detail, this is what I am trying to replicate: http://www.broadinstitute.org/cmap/help_topics_linkified.jsp (lots of examples of this plot at the bottom of the page)
EDIT (based on comments): The y-axis values are ranks that change with the score column. The colors represent a grouping with positive score values in green, negative in red, and black lines for a set of "selected" rows. This is not a stacked bar plot. The values on the y-axis (be it rank or score) are not cumulative and the group region for the "selected" (black) group can be distributed across the other three group regions (as shown in example data below).
Example:
structure(list(group = structure(c(1L, 1L, 4L, 1L, 1L, 3L, 3L,
4L, 3L, 2L, 4L, 2L, 2L), .Label = c("positive", "negative", "null",
"selected"), class = "factor"), rank = c(1, 2, 3, 4, 5, 7.5,
7.5, 7.5, 7.5, 10, 11, 12, 13), xaxis = c(1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1), score = c(0.85, 0.7, 0.55, 0.4, 0.25, 0, 0,
0, 0, -0.5, -0.65, -0.8, -0.95)), .Names = c("group", "rank",
"xaxis", "score"), row.names = c(NA, -13L), class = "data.frame")
group rank xaxis score
1 positive 1.0 1 0.85
2 positive 2.0 1 0.70
3 selected 3.0 1 0.55
4 positive 4.0 1 0.40
5 positive 5.0 1 0.25
6 null 7.5 1 0.00
7 null 7.5 1 0.00
8 selected 7.5 1 0.00
9 null 7.5 1 0.00
10 negative 10.0 1 -0.50
11 selected 11.0 1 -0.65
12 negative 12.0 1 -0.80
13 negative 13.0 1 -0.95
I tried the following but I am looking for a bar or rectangle, not points.
ggplot(df, aes(xaxis,rank,colour=group)) +
geom_point(size=3) +
scale_colour_manual(values=c("positive"="green", "negative"="red", "null"="grey", "selected"="black")) +
theme_bw() + scale_y_reverse() + scale_x_discrete(breaks=NULL)
stacked geom_bar() and geom_rect() don't seem to work with continuous y values.
Any help would be appreciated. Thanks!
UPDATE (using #bjoseph's solution to replicate the exact plot shown in the link above)
df$size = as.factor(1)
df$height = 1
ggplot(df, aes(1,x=size,y=height,fill=group,group=rank)) +
geom_bar(stat='identity') + science_theme +
scale_fill_manual(values=c("positive"="green", "negative"="red", "null"="grey", "selected"="black")) + theme_bw() +
scale_y_reverse(breaks=NULL) + scale_x_discrete(breaks=NULL)
This works
df = structure(list(group = structure(c(1L, 1L, 4L, 1L, 1L, 3L, 3L,
4L, 3L, 2L, 4L, 2L, 2L), .Label = c("A", "B", "C", "D"), class = "factor"),
value = 1:13), .Names = c("group", "value"), row.names = c(NA,
-13L), class = "data.frame")
df$size=as.factor(1)
df$height=1
ggplot(df, aes(1,x=size,y=height,fill=group,group=value)) +
geom_bar(stat='identity',color="black") +
theme_bw()
It produces the attached plot.
The color="black" command inside geom_bar produces black outlines around your groups. You can also suppress or manually label the y-axis if you need/want.
I have a problem with the aes parameters; not sure what it's trying to tell me to fix.
My data frame is like so:
> qualityScores
Test1 Test2 Test3 Test4 Test5
Sample1 1 2 2 3 1
Sample2 1 2 2 3 2
Sample3 1 2 1 1 3
Sample4 1 1 3 1 1
Sample5 1 3 1 1 2
Where 1 stands for PASS, 2 for WARN, and 3 for FAIL.
Here's the dput of my data:
structure(list(Test1 = c(1L, 1L, 1L, 1L, 1L), Test2 = c(2L, 2L,
2L, 1L, 3L), Test3 = c(2L, 2L, 1L, 3L, 1L), Test4 = c(3L, 3L,
1L, 1L, 1L), Test5 = c(1L, 2L, 3L, 1L, 2L)), .Names = c("Test1",
"Test2", "Test3", "Test4", "Test5"), class = "data.frame", row.names = c("Sample1",
"Sample2", "Sample3", "Sample4", "Sample5"))
I am trying to make a heatmap where 1 will be represented by gree, 2 by yellow, and 3 by red, using ggplots2.
This is my code:
samples <- rownames(qualityScores)
tests <- colnames(qualityScore)
testScores <- unlist(qualityScores)
colors <- colorRampPalette(c("red", "yellow", "green"))(n=3)
ggplot(qualityScores, aes(x = tests, y = samples, fill = testScores) ) + geom_tile() + scale_fill_gradient2(low = colors[1], mid = colors[2], high = colors[3])
And I get this error message:
Error: Aesthetics must either be length one, or the same length as the dataProblems:colnames(SeqRunQualitySumNumeric)
Where am I going wrong?
Thank you.
This will be much easier if your reshape your data from wide to long format. There are many ways to do that but here I used reshape2
library(reshape2); library(ggplot2)
colors <- c("green", "yellow", "red")
ggplot(melt(cbind(sample=rownames(qualityScores), qualityScores)),
aes(x = variable, y = sample, fill = factor(value))) +
geom_tile() +
scale_fill_manual(values=colors)
I'm trying to have ggplot2 plot percentage instead of frequency on the y axis but it just doesn't work! I have added scale_y_continuous(labels = percent_format()) to my plot but it still displays frequencies. Here's my code:
ggplot(items) + geom_bar( aes(x = type, fill = category), position = "dodge") + scale_y_continuous(labels = percent_format())
Here is a sample of my dataset
head(items)
item type category
[1] PA100 1 A
[2] PB101 2 A
[3] UR360 2 A
[4] PX977 3 B
[5] GA008 3 B
[6] GR446 3 A
What I want to do is for each category A and B I want to plot the percentage of type 1, type 2, and type 3 it has; hence my code. But no matter what it keeps plotting the frequencies of type 1, 2, and 3 in each of the categories instead of the percentages :|
Try:
items = structure(list(item = structure(c(3L, 4L, 6L, 5L, 1L, 2L), .Label = c("GA008",
"GR446", "PA100", "PB101", "PX977", "UR360"), class = "factor"),
type = c(1L, 2L, 2L, 3L, 3L, 3L), category = structure(c(1L,
1L, 1L, 2L, 2L, 1L), .Label = c("A", "B"), class = "factor")), .Names = c("item",
"type", "category"), class = "data.frame", row.names = c(NA,
-6L))
items
item type category
1 PA100 1 A
2 PB101 2 A
3 UR360 2 A
4 PX977 3 B
5 GA008 3 B
6 GR446 3 A
tt = with(items, table(type, category))
tt
category
type A B
1 1 0
2 2 0
3 1 2
dd = data.frame(prop.table(tt, 2))
dd
type category Freq
1 1 A 0.25
2 2 A 0.50
3 3 A 0.25
4 1 B 0.00
5 2 B 0.00
6 3 B 1.00
ggplot(dd)+geom_bar(aes(x=type, y=Freq, fill=category), stat='identity', position='dodge')