Add trend line to longitudinal data line graph - r

How can I add a trend line that represents the average values for the groups in red and black? I have tried geom_line() and geom_smooth() and haven't gotten it to work.
Here is the code I used to make this graph:
ggplot(data = long_data, aes(x = day, y = val, group = ID, colour = outcome)) +
geom_line(aes(color = as.factor(outcome), linetype = as.factor(outcome)), size = 0.7) +
scale_linetype_manual(values = c("longdash", "solid")) +
scale_color_manual(values = c("black", "red")) + theme_bw() +
theme(legend.position = "", panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
labs(x = "day", y = var, colour = "outcome ") +
xlim(0, 25) +
ggtitle(var) +
theme(plot.title = element_text(hjust = 0.5)) +
ylim(0,100)

Your grouping variable is ID, that's what geom_smooth will inherit.
Try specifying your desired grouping variable outcome by using geom_smooth(aes(group = outcome))

Related

Color dataset by group and add geom_vline to legend only

I have a genome-wide dataset that I'm trying to plot in the following way:
Have each chromosome be a separate color
Have specific windows highlighted by a bar (I'm using geom_vline) - this I'm getting from a
separate table
Have only geom_vline feature in the legend
I have tried many different things, but it seems I cannot have all three together!
Here is the link to both datasets:
allStats & allStats_fstPi_group15
With this code, I can have the first 2, but not the 3rd:
ggplot(allStats, aes(x = mid2, y = Fst_group1_group5,
color = as_factor(scaffold))) +
geom_point(size = 2) +
geom_vline(xintercept = chrom$add, color = "grey") +
scale_y_continuous(expand = c(0,0), limits = c(0, 1)) +
scale_x_continuous(labels = chrom$chrID, breaks = axis_set$center) +
scale_color_manual(values = rep(c("#276FBF", "#183059"), unique(length(chrom$chrID)))) +
scale_size_continuous(range = c(0.5,3)) +
labs(x = NULL,
y = "Fst SBM vs OC") +
theme_minimal() +
theme(
legend.position = "none",
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
axis.title.y = element_text(),
axis.text.x = element_text()) +
geom_vline(data = allStats_fstPi_group15,
aes(xintercept = allStats_fstPi_group15$mid2),
color = "orange", show.legend = T)
With this one I can get 2 and 3 only (I'm not able to color code each block separately):
cols <- c("SBM vs OC" = rep(c("#276FBF", "#183059"), unique(length(chrom$chrID))),
"90th percentile (Fst vs Pi)" = "orange")
ggplot(allStats, aes(x = mid2)) +
geom_point(aes(y = Fst_group1_group5,
color = as_factor(scaffold)),
size = 2) +
geom_vline(data = allStats_fstPi_group15,
aes(xintercept = allStats_fstPi_group15$mid2,
color = "90th percentile (Fst vs Pi)")) +
scale_color_manual(values = cols)
I've seen the issue with the legend being that color needs to be within aes(), so my question is: is it impossible what I'm trying to do?

How to set a conditional size scale based on name in ggplot?

Below is a simple bubble plot for three character traits (Lg_chr, Mid_chr, and Sm_chr) across three locations.
All good, except that because the range of Lg_chr is several orders of magnitude larger than the ranges for the other two traits, it swamps out the area differences between the smaller states, making the differences very difficult to see - for example, the area of the points for for Location_3's Mid_chr (70) and Sm_chr (5), look almost the same.
Is there a way to set a conditional size scale based on name in ggplot2 without having to facit wrap them? Maybe a conditional statement for scale_size_continuous(range = c(<>, <>)) separately for Lg_chr, Mid_chr, and Sm_chr?
test_df = data.frame(lg_chr = c(100000, 150000, 190000),
mid_chr = c(50, 90, 70),
sm_chr = c(15, 10, 5),
names = c("location_1", "location_2", "location_3"))
#reformat for graphing
test_df_long<- test_df %>% pivot_longer(!names,
names_to = c("category"),
values_to = "value")
#plot
ggplot(test_df_long,
aes(x = str_to_title(category),
y = str_to_title(names),
colour = str_to_title(names),
size = value)) +
geom_point() +
geom_text(aes(label = value),
colour = "white",
size = 3) +
scale_x_discrete(position = "top") +
scale_size_continuous(range = c(10, 50)) +
scale_color_manual(values = c("blue", "red",
"orange")) +
labs(x = NULL, y = NULL) +
theme(legend.position = "none",
panel.background = element_blank(),
panel.grid = element_blank(),
axis.ticks = element_blank()) ```
Edit:
You could use ggplot_build to manually modify the point layer [[1]] to specify the sizes of your points like this:
#plot
p <- ggplot(test_df_long,
aes(x = str_to_title(category),
y = str_to_title(names),
colour = str_to_title(names),
size = value)) +
geom_point() +
geom_text(aes(label = value),
colour = "white",
size = 3) +
scale_x_discrete(position = "top") +
scale_color_manual(values = c("blue", "red",
"orange")) +
labs(x = NULL, y = NULL) +
theme(legend.position = "none",
panel.background = element_blank(),
panel.grid = element_blank(),
axis.ticks = element_blank())
q <- ggplot_build(p)
q$data[[1]]$size <- c(7,4,1,8,5,2,9,6,3)*5
q <- ggplot_gtable(q)
plot(q)
Output:
You could use scale_size with a log10 scale to make the difference more visuable like this:
#plot
ggplot(test_df_long,
aes(x = str_to_title(category),
y = str_to_title(names),
colour = str_to_title(names),
size = value)) +
geom_point() +
geom_text(aes(label = value),
colour = "white",
size = 3) +
scale_size(trans="log10", range = c(10, 50)) +
scale_x_discrete(position = "top") +
scale_color_manual(values = c("blue", "red",
"orange")) +
labs(x = NULL, y = NULL) +
theme(legend.position = "none",
panel.background = element_blank(),
panel.grid = element_blank(),
axis.ticks = element_blank())
Output:

How to plot counts stackbar in ggplot2 R?

Dataset contains "two friends" and coded "interaction" (all factors). I want to plot the frequency of type of interactions between two friends using a stacked bar. I tried the following code.
Friend1 <- c("A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B")
Friend2 <- c("1","1","2","2","1","1","2","2","1","1","2","2","1","1","2","2")
Interaction <- c("O","X","D","D","D","X","X","D/R","O","X","D","D","D","X","X","D/R")
df <- data.frame(Friend1, Friend2, Interaction)
df$Friend1 <- as.factor(as.character(df$Friend1))
df$Friend2 <- as.factor(as.character(df$Friend2))
df$Interaction <- as.factor(as.character(df$Interaction))
ggplot(df, aes(fill=Interaction, y=count(Interaction), x=Friend2)) +
geom_bar(position="fill", stat="identity", color = "white") + theme_classic() + theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(colour = "black", size=1)) + theme(strip.background = element_blank()) + facet_grid(.~Friend1)
Erorr: Error in UseMethod("count") :
no applicable method for 'count' applied to an object of class "character"
How do I "count" these factors to visualize frequency of interactions?
The issue is that dplyr::count expects a dataframe as its first argument and returns a dataframe. However, there is no reason to compute the counts as geom_bar will do that by default, i.e. get rid of y=... and stat="identity":
library(ggplot2)
ggplot(df, aes(fill = Interaction, x = Friend2)) +
geom_bar(position = "fill", color = "white") +
theme_classic() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(colour = "black", size = 1)
) +
theme(strip.background = element_blank()) +
facet_grid(. ~ Friend1)
An alternative visualization using facets per "friends" column may make your counts clearer than a standard stacked bar:
ggplot(df, aes(x = 1, fill = Interaction)) +
geom_bar(width = 1, color = "white", size = 1, alpha = 0.8) +
geom_text(stat = "count", aes(label = after_stat(count)), size = 7,
position = position_stack(vjust = 0.5), color = "white",
fontface = 2) +
facet_grid(Friend1 ~ Friend2, switch = "both") +
scale_fill_brewer(palette = "Set1") +
coord_polar(theta = "y") +
labs(x = "Friend1", y = "Friend2") +
theme_bw(base_size = 20) +
theme(panel.grid = element_blank(),
strip.background = element_blank(),
strip.placement = "outside",
axis.text.x = element_blank(),
panel.border = element_rect(color = "gray90", fill = NA),
panel.spacing = unit(0, "mm"),
axis.text = element_blank(),
axis.ticks = element_blank())

Changing the figure legend to indicate the line type... (ggplot2)

I have the formula below:
ggplot(Errortrialsmodifyoriginal, aes(x = Target, y = Absolutefirststoperror, color = as.character(Type), shape = as.character(Type))) +
geom_point(shape=16)+
geom_point(data=Errortrialoriginal,shape=19,size = 4,mapping=aes(x=Target, y=Absolutefirststoperror)) +
geom_line(data=Errortrialoriginal,aes(group=Type,linetype=Type),size=2,) +
scale_color_manual(name = "Condition", values = c("red","green","blue","red","green","blue")) +
scale_linetype_manual(name = "Condition",values = c("dashed","dashed","dashed","solid","solid","solid")) +
geom_errorbar(data=Errortrialoriginal,mapping=aes(x=Target, ymin=Absolutefirststoperror-SE,ymax=Absolutefirststoperror+SE),size=0.5) +
theme_bw() + guides(color = guide_legend("Condition"), shape = guide_legend("Condition"), linetype = guide_legend("Condition")) +
labs(x = "Target distance (vm)", y = "Absolute error in stop location (vm)") +
theme(axis.title.x = element_text(size=14, face="bold"), axis.title.y = element_text(size=14, face="bold"),legend.text=element_text(size=14),title=element_text(size=14,face="bold"), plot.title = element_text(hjust = 0.5), legend.title = element_text(size=14,face="bold"), axis.text.x=element_text(size=14),axis.text.y=element_text(size=14),panel.grid.major = element_blank(), panel.grid.minor = element_blank())
Which produces the graph:
How can I change my command to ensure that the dashed and solid lines are shown in the figure legend; because at the moment, the figure legend suggests that all the lines are solid, even though they are not?
I would be grateful for any advice!
To my opinion, the legend is correctly displayed but you can't see it because you have big points front of the linetype. You should increase the legend box to see it.
Here an example with this dummy example:
library(ggplot2)
ggplot(my_data, aes(x = dose, y = length, color = supp, linetype = supp))+
geom_line()+
geom_point(size = 4)
library(ggplot2)
ggplot(my_data, aes(x = dose, y = length, color = supp, linetype = supp))+
geom_line()+
geom_point(size = 4)+
theme(legend.key.size = unit(3,"line"))
So, with your code, you can do something like that:
library(ggplot2)
ggplot(Errortrialsmodifyoriginal,
aes(x = Target,
y = Absolutefirststoperror,
color = Type)) +
geom_point()+
geom_line(data=Errortrialoriginal,
aes(group=Type,
linetype=Type)) +
scale_color_manual(name = "Condition", values = rep(c("red","green","blue"),2)) +
scale_linetype_manual(name = "Condition",values = rep(c("dashed","solid"),each =3)) +
geom_errorbar(data=Errortrialoriginal,
mapping=aes(x=Target,
ymin=Absolutefirststoperror-SE,
ymax=Absolutefirststoperror+SE),size=0.5) +
theme_bw() +
guides(color = guide_legend("Condition"), shape = guide_legend("Condition"), linetype = guide_legend("Condition")) +
labs(x = "Target distance (vm)", y = "Absolute error in stop location (vm)") +
theme(axis.title.x = element_text(size=14, face="bold"),
axis.title.y = element_text(size=14, face="bold"),
legend.text=element_text(size=14),
title=element_text(size=14,face="bold"),
plot.title = element_text(hjust = 0.5),
legend.title = element_text(size=14,face="bold"),
axis.text.x=element_text(size=14),
axis.text.y=element_text(size=14),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.key.size = unit(3,"line"))
Does it answer your question ?
If not, please consider providing a reproducible example of your dataset (see: How to make a great R reproducible example)

change line type in ggplot2 in r

I have a data frame like this.
df <- data.frame(date = c('2015-11-23','2015-11-24','2015-11-25','2015-11-23','2015-11-24','2015-11-25'),
variable = c('LCNB', 'LCNB','LCNB','LCDEF','LCDEF','LCDEF'),
value = c(1,2,3,3,2,1))
I want to plot two lines in the same plot, with different color and line types. my current code is:
library(scales)
ggplot(df, aes(x=as.Date(date), y=value, color=variable)) + geom_line(size=1.07) +
scale_color_manual(labels = c("Nb",'Def'), values = c("#E69F00", "#0072B2")) +
scale_x_date(labels = date_format("%Y-%m-%d"), breaks = date_breaks("2.8 month")) +
theme(axis.text.x = element_text(angle = 0, vjust = 0.5, hjust=1))+labs(x="Dates",y="%") +
theme_bw()+
theme( panel.grid.major = element_blank(),panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),plot.margin=unit(c(0,1,0.3,1), "cm")) +
labs(colour = "LC") + theme(legend.position = c(0.95,0.85))
my code so far only makes two lines different color, how can i make them differnt line types as well.
Thank you for the help,
You just need to take the same steps with line type as you did with color, but for linetype:
ggplot(df, aes(x=date, y=value, color=variable, linetype = variable)) +
geom_line(size=1.07,) +
scale_color_manual(
labels = c("Nb",'Def'),
values = c("#E69F00", "#0072B2")
) +
scale_linetype(labels = c("Nb", "Def")) +
scale_x_date(labels = date_format("%Y-%m-%d"), breaks = date_breaks("2.8 month")) +
theme(axis.text.x = element_text(angle = 0, vjust = 0.5, hjust=1)) +
labs(x="Dates",y="%", colour = "LC", linetype = "LC") +
theme_bw()+
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
plot.margin=unit(c(0,1,0.3,1), "cm"),
legend.position = c(0.95,0.85))

Resources