Related
I already googled a lot on this problem, but none of the suggested solutions worked:
The plot shows actual and expected values for each subject on four sessions ('sitz' 1 to 4). The actual value and the expected value come from two different variables, actual value has three levels 'cont', 'treat and 'other', expected value has two levels:'cont' and 'treat'.
The actual value is depicted by a filled black circle and the expected one by an unfilled blue circle.
The problem: I don't find any method so that my legend is printed correctly. The best I could get was a black and filled and a blue and filled button, but the blue one shouldn't be filled.
Here's the data and code:
df <- structure(list(vp = c(2, 5, 15, 28, 32, 2, 18, 7, 28, 16, 24,
6, 16, 22, 25, 26, 28, 14, 4, 8, 15, 38, 21, 29, 26, 21, 21,
12, 11, 23), sitz = structure(c(3L, 2L, 4L, 1L, 4L, 3L, 4L, 2L,
2L, 1L, 4L, 3L, 3L, 3L, 3L, 2L, 1L, 1L, 2L, 3L, 1L, 3L, 3L, 4L,
1L, 4L, 2L, 2L, 4L, 4L), .Label = c("1", "2", "3", "4"), class = "factor"),
GROUP = structure(c(2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L,
1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L,
2L, 2L, 2L, 2L, 2L), .Label = c("cont", "treat"), class = "factor"),
img_50group = structure(c(3L, 3L, 3L, 2L, 3L, 3L, 3L, 3L,
3L, 1L, 1L, 3L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 1L, 1L, 1L,
1L, 1L, 3L, 3L, 3L, 3L, 3L), .Label = c("cont", "other",
"treat"), class = "factor")), row.names = c(NA, -30L), class = "data.frame")
ggplot(df, aes(x = sitz, y = img_50group)) +
geom_point(aes(x = sitz, y = img_50group, color = "black", shape = 16), size = 2) +
geom_point(aes(x = sitz, y = GROUP, color = "darkblue", shape = 1), size = 5) +
facet_wrap(~vp) +
scale_colour_manual(name="Strategies", labels = c("Actual", "Expected"), values=c("black", "darkblue")) +
scale_shape_identity(name="Strategies", labels = c("Actual", "Expected"), breaks =c("black", "darkblue"), guide = "legend") ```
You should melt you data and then use color, shape, and size as aesthetics in the aes() call in geom_point().
library(tidyverse)
df <- structure(list(vp = c(2, 5, 15, 28, 32, 2, 18, 7, 28, 16, 24,
6, 16, 22, 25, 26, 28, 14, 4, 8, 15, 38, 21, 29, 26, 21, 21,
12, 11, 23), sitz = structure(c(3L, 2L, 4L, 1L, 4L, 3L, 4L, 2L,
2L, 1L, 4L, 3L, 3L, 3L, 3L, 2L, 1L, 1L, 2L, 3L, 1L, 3L, 3L, 4L,
1L, 4L, 2L, 2L, 4L, 4L), .Label = c("1", "2", "3", "4"), class = "factor"),
GROUP = structure(c(2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L,
1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L,
2L, 2L, 2L, 2L, 2L), .Label = c("cont", "treat"), class = "factor"),
img_50group = structure(c(3L, 3L, 3L, 2L, 3L, 3L, 3L, 3L,
3L, 1L, 1L, 3L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 1L, 1L, 1L,
1L, 1L, 3L, 3L, 3L, 3L, 3L), .Label = c("cont", "other",
"treat"), class = "factor")), row.names = c(NA, -30L), class = "data.frame")
# Melt the data frame and use factors to preserve the order in the original graph
df <- df %>% pivot_longer(cols = c(-vp, -sitz)) %>%
mutate(value = fct_relevel(value, "cont", "other", "treat"),
name = factor(name, levels = c("img_50group", "GROUP")))
ggplot(df, aes(x = sitz, y = value)) +
geom_point(aes(x = sitz, y = value, color = name, shape = name, size = name)) +
facet_wrap(~vp) +
scale_colour_manual(name="Strategies",
labels = c("Actual", "Expected"),
values=c("black", "darkblue")) +
scale_shape_manual(name="Strategies",
labels = c("Actual", "Expected"),
values = c(16, 1)) +
scale_size_manual(name ="Strategies",
labels = c("Actual", "Expected"),
values = c(2, 5))
Created on 2020-04-09 by the reprex package (v0.3.0)
I am having trouble getting my plots to work, I have multiple categorical variables by which I want to color by one, and facet by another. However, R keeps adding the "values" (I used melt) for the same variables together instead. It works when I only have one variable.
Here is my plot with one variable
Here is my plot with two variables, you can see the adding that is happening
simple dataframe
Here is my code:
library(reshape2)
library(ggplot2)
test2 <- structure(list(SampleID = c(12.19, 12.22, 13.1, 12.19, 12.22,
13.1, 12.19, 12.22, 13.1, 12.19, 12.22, 13.1), patient = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), type = structure(c(1L,
1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L), .Label = c("L",
"T"), class = "factor"), timepoint = structure(c(1L, 2L, 2L,
1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L), .Label = c("1", "2"), class = "factor"),
Group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = "D", class = "factor"), variable = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), .Label = c("A",
"B", "C", "D", "E", "F", "G", "H", "I"), class = "factor"),
value = c(2L, 5L, 6L, 25L, 18L, 12L, 6L, 10L, 15L, 21L, 23L,
33L)), .Names = c("SampleID", "patient", "type", "timepoint",
"Group", "variable", "value"), row.names = c(NA, 12L), class = "data.frame")
ggplot(test2, aes(test2$variable, test2$value, fill=test2$timepoint)) +
geom_bar(stat="identity", position = "dodge") +
scale_fill_manual(values=c("rosybrown1", "steelblue2", "gray")) +
labs(x="Category", y="Count", title = paste0("Sample ", as.character(unique(test2$patient)) , " - " , as.character(unique(test2$Group)))) +
facet_wrap(~test2$type) +
theme(text = element_text(size=15),
axis.text.x = element_text(angle = 90, hjust = 1, vjust=.5, size = 7))
If I am understanding right, it looks like you just need to give the scales option to facet_wrap like so:
facet_wrap(~type, scales = "free_x")
I am trying to understand graphs and I am using toy dataset as follows
df = structure(list(A = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("0","1"), class = "factor"),
B= structure(c(1L, 1L,2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("0","1"), class = "factor"),
C= structure(c(1L,1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("0","1"), class = "factor"),
D= structure(c(1L,1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("0","1"), class = "factor"),
Weight = c(12, 33, 65, 72, 9, 3, 3, 10, 17, 9, 9, 25, 1, 0, 1, 4)), .Names = c("A","B", "C", "D", "Weight"),
row.names = c(NA, -16L), class = "data.frame")
I am interested in converting this adjacency matrix above to a From and To matrix something like this.
From To Weight
A B 72
A C 3
C D 1
. . .
. . .
A D 9
So on, any help on how to accomplish this is much appreciated.
-------------user20650 correction-----------
df = df[-1,] excluding the first observation
Is there a way to make the thinner lines in the plot (those without an y axis tick label) appear closer to the lines above (those with a label) so as to better simulate pairs of baseline / actual bars of the same activity in a gantt chart?
See gantt chart examples here and here.
mdfr <- structure(list(name = structure(c(8L, 8L, 8L, 8L, 6L, 6L, 6L,
6L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 7L, 7L, 7L, 7L, 5L, 5L, 5L,
5L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 8L, 8L, 8L, 8L, 6L, 6L, 6L,
6L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 7L, 7L, 7L, 7L, 5L, 5L, 5L,
5L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L), .Label = c("100 A", "100 B",
"101 A", "101 B", "102 A", "102 B", "103 A", "103 B"), class = "factor"),
stadio = c(2, 4, 5, 7, 2, 4, 5, 7, 2, 4, 5, 7, 2, 4, 5, 7,
1, 3, 6, 8, 1, 3, 6, 8, 1, 3, 6, 8, 1, 3, 6, 8, 2, 4, 5,
7, 2, 4, 5, 7, 2, 4, 5, 7, 2, 4, 5, 7, 1, 3, 6, 8, 1, 3,
6, 8, 1, 3, 6, 8, 1, 3, 6, 8), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L), .Label = c("start_date", "end_date"), class = "factor"),
value = c("05/10/2012", "17/12/2012", "12/03/2012", "30/05/2013",
"10/01/2013", "14/10/2013", "24/10/2013", "10/01/2014", "30/09/2013",
"29/01/2014", "30/01/2014", "06/05/2014", "30/09/2013", "29/01/2014",
"30/01/2014", "06/05/2014", "05/10/2012", "17/12/2012", "12/03/2012",
"30/05/2013", "10/01/2013", "14/10/2013", "24/10/2013", "10/01/2014",
"30/09/2013", "29/01/2014", "30/01/2014", "05/06/2014", "30/09/2013",
"29/01/2014", "30/01/2014", "05/06/2014", "17/12/2012", "12/03/2012",
"30/05/2013", "30/05/2014", "14/10/2013", "24/10/2013", "10/01/2014",
"11/07/2014", "29/01/2014", "30/01/2014", "06/05/2014", "23/12/2014",
"29/01/2014", "30/01/2014", "06/05/2014", "23/12/2014", "17/12/2012",
"12/03/2012", "30/05/2013", "30/05/2014", "14/10/2013", "24/10/2013",
"10/01/2014", "11/07/2014", "29/01/2014", "30/01/2014", "05/06/2014",
"28/12/2014", "29/01/2014", "30/01/2014", "05/06/2014", "29/12/2014"
), rating = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("3",
"5"), class = "factor")), row.names = c(NA, -64L), .Names = c("name",
"stage", "variable", "value", "rating"), class = "data.frame")
names <- as.character(unique(mdfr$name))
names1 <- names[gsub("[^ B]","",names) == " B"]
names1 <- paste("No.",gsub("[ B]","",names1),sep="")
names2 <- rep("",length(names1))
new.names <- c(names1,names2)
ggplot(mdfr, aes(as.POSIXct(as.Date(value, "%d/%m/%Y")), name, colour = factor(stage))) +
geom_line(aes(size=rating)) +
labs(colour="(Baseline/Actual):", x = "", y = "") +
scale_colour_brewer(palette="RdYlGn",breaks = c("1", "3", "6","8"), guide = "none") +
scale_size_manual(breaks = levels(mdfr$rating), values = as.integer(levels(mdfr$rating)), guide = "none") +
scale_y_discrete(breaks=names, labels=new.names)
I would use facets to do this. Below you find a possible solution. This may not be the most elegant solution, but it lets you change the distance between thinner and thicker lines by changing the expand argument in scale_x_discrete.
# numbers to facet by (levels used for order of the facets)
mdfr$nr <- factor(paste0("No.", as.numeric(gsub("A|B", "", mdfr$name))),
levels=unique(paste0("No.", as.numeric(gsub("A|B", "", mdfr$name)))))
# recast your data
df <- dcast(mdfr, nr+stage+rating~variable)
# plot as before, switched x and y values
ggplot(df, aes(x=factor(rating),
ymin=as.POSIXct(as.Date(start_date, "%d/%m/%Y")),
ymax=as.POSIXct(as.Date(end_date, "%d/%m/%Y")),
color=factor(stage),
size=rating
)) +
geom_linerange() + # linerange instead of line
facet_grid(nr~., scales="free_x") + # faceting
coord_flip() + # flip coordinates back
scale_x_discrete(name="", breaks=NULL, expand=c(4,1)) + # use the expand variable to change the distances
scale_colour_brewer(palette="RdYlGn",breaks = c("1", "3", "6","8"), guide = "none") +
scale_size_manual(breaks = levels(mdfr$rating), values = as.integer(levels(mdfr$rating)), guide = "none")
I make a ggplot2 graphic like this:
ggplot(dat, aes(x=timepoint, y=y, size=Status, shape=Status)) +
geom_point(fill="red") +
geom_smooth(method=lm, se=FALSE, size=1, linetype="twodash") +
facet_grid(Test ~ Batch, scales="free_y")
And it gives:
There are other options in my code to control legend appearance, etc, but nowhere I have asked geom_smooth() to exclude some values, and as you can see it automatically excludes the points having Status=="FAIL" !
You can generate such a plot without the other options (but showing the same problem) with the following dataframe:
dat <- structure(list(Test = structure(c(2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("PH",
"ANTIGENIC ACTIVITY BY ELISA", "WATER CONTENT BY µKARL FISCHER"
), class = "factor"), Batch = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("HB07",
"HB08"), class = "factor"), timepoint = c(0, 1, 2, 3, 0, 1, 2,
3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3), Status = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("PASS", "FAIL"), class = "factor"), y = c(11.7,
12.7, 12.8, 17.6, 8.6, 9.6, 16.5, 15.4, 10.1, 9, 11.7, 12.5,
7.9, 9.3, 15.5, 14.7, 12.9, 10.8, 14.5, 16.5)), .Names = c("Test",
"Batch", "timepoint", "Status", "y"), row.names = c(NA, -20L), class = "data.frame")
By providing the shape aes you group the data. If the group consists only of one member, geom_smooth cannot print anything for that group.
Possibly you want this:
geom_smooth(aes(shape=NA),method=lm, se=FALSE, size=1, linetype="twodash")