I want to create a line plot with the shapes varied by the Methods variable in my dataset. However, I don't want the lines to be drawn over the shapes, for example this is not good:
How do I hide the lines behind the shapes so that the lines are not through the shapes. Here is the dataset:
xx <- data.frame(
stringsAsFactors = FALSE,
rho = c(0.7,0.7,0.7,0.7,0.7,0.7,
0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,
0.7,0.7,0.7,0.7,0.7,0.7),
sample = c(1L,1L,1L,1L,1L,1L,1L,1L,
2L,2L,2L,2L,2L,2L,2L,2L,3L,3L,3L,3L,3L,3L,
3L,3L),
tp = c(10L,10L,10L,10L,20L,20L,
20L,20L,10L,10L,10L,10L,20L,20L,20L,20L,10L,10L,
10L,10L,20L,20L,20L,20L),
Methods = c("lmm","residualboot",
"clustboot","mbbboot","lmm","residualboot","clustboot",
"mbbboot","lmm","residualboot","clustboot","mbbboot",
"lmm","residualboot","clustboot","mbbboot","lmm",
"residualboot","clustboot","mbbboot","lmm","residualboot",
"clustboot","mbbboot"),
fixinterbias = c(-0.07069111,-0.08709062,
-0.13675904,-0.03077662,-0.2093937,-0.2092973,0.2344589,
-0.1650586,-0.08666544,-0.09681292,0.05795378,
-0.08564713,-0.015873476,-0.022712667,-0.090171359,
0.001930576,0.03720186,0.04073916,-0.08692844,0.04538355,
-0.09867106,-0.09874304,-0.08654507,-0.1161617),
fixslopebias = c(0.06225352,0.06467038,
0.06003106,0.05557157,-0.01036622,-0.01039492,-0.083628,
-0.01530608,0.02736118,0.02863767,0.04872466,0.02607667,
0.08056533,0.08076664,0.09773794,0.07819871,
-0.0703907,-0.07103784,-0.06005637,-0.07246422,0.0189303,
0.01863365,0.0145846,0.02057585)
)
And here is my code:
fixinter <- ggplot(xx, aes(x=sample, y=fixinterbias, shape=Methods, linetype=Methods)) +
geom_line(aes(color=Methods), size = 0.75) +
geom_hline(yintercept=0, linetype="dashed", color = "black") +
scale_shape_manual(values=c(0, 1, 2, 5)) +
scale_x_continuous(name="Sample size (n)", breaks = c(1, 2, 3), label = c(20, 50, 100)) +
scale_y_continuous(name="fix-effect intercept bias") +
geom_point(aes(color=Methods, shape = Methods),
stroke = 1.0, fill = "white") +
theme_classic()
fixinter + facet_grid(tp ~. )
You can use the "filled" version of each shape (use for example ggpubr::show_point_shapes() to see a list), so here 22, 21, 24 and 23.
fixinter <- ggplot(xx, aes(x=sample, y=fixinterbias, shape=Methods, linetype=Methods)) +
geom_line(aes(color=Methods), size = 0.75) +
geom_hline(yintercept=0, linetype="dashed", color = "black") +
scale_shape_manual(values=c(22, 21, 24, 23)) +
scale_x_continuous(name="Sample size (n)", breaks = c(1, 2, 3), label = c(20, 50, 100)) +
scale_y_continuous(name="fix-effect intercept bias") +
geom_point(aes(color=Methods, shape = Methods),
stroke = 1.0, fill = "white") +
theme_classic()
fixinter + facet_grid(tp ~. )
Related
I have created a random walk plot using ggplot2 (code below). I wondered if it would be possible to use the gganimate package so that the random walk process (the black line in the plot) gradually appears but stops once it touches the grey horizontal dashed line.
set.seed(3344)
create_random_walk <- function(number=500){
data.frame(x = rnorm(number),
rown = c(1:500)) %>%
mutate(xt = cumsum(x))
}
randomwalkdata <- rbind(mutate(create_random_walk(), run = 1))
p <- ggplot(randomwalkdata, aes(x = rown, y = xt)) +
geom_line() +
labs(x = '\nTime (arbitrary value)', y = 'Evidence accumulation\n') +
theme_classic()
p + geom_segment(aes(x = 0.5, xend = 500, y = 25, yend = 25, linetype = 2), colour = "grey", size = 1, show.legend = FALSE) +
scale_linetype_identity()
Can anybody help?
library(gganimate); library(dplyr)
animate(
ggplot(randomwalkdata |> filter(cumsum(lag(xt, default = 0) >= 25) == 0),
aes(x = rown, y = xt)) +
geom_line() +
geom_point(data = . %>% filter(rown == max(rown)),
size = 10, shape = 21, color = "red", stroke = 2) +
labs(x = '\nTime (arbitrary value)', y = 'Evidence accumulation\n') +
theme_classic() +
annotate("segment", x = 0.5, xend = 500, y = 25, yend = 25, linetype = 2,
colour = "grey", linewidth = 1) +
scale_linetype_identity() +
transition_reveal(rown),
end_pause = 20, width = 600)
I'm trying to add a legend to my graph which consists of two lines. Unfortunately, I don't understand how. Could you help me out, please? My dataframe can be found below.
Thank you.
# dput(df)
structure(list(t = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), Beta_Treatment = c(0.999550283587599,
1.1284848818908, 1.3173797973744, 1.381630131904, 1.42558575749304,
1.598454651784, 1.68182321476, 1.908913645904, 1.9830406566956,
2.0704834391088), Beta_Control = c(0.822042496772398, 0.914170557749195,
0.812776816705598, 0.829879870335997, 0.906381000106958, 0.891107911295998,
0.885094437239998, 0.810487561695997, 0.816917509704399, 0.881900118891192
), treatment_stderr = c(0.0466078055173255, 0.0451188571313495,
0.0473857585047991, 0.0465872903741648, 0.0436607368165115, 0.0479028140228299,
0.0445418661900949, 0.0451947124622918, 0.0474264037378467, 0.0464878615840165
), control_stderr = c(0.0329566953375247, 0.0319038498369643,
0.0335067911704116, 0.0329421889406788, 0.0308728030745565, 0.0338724046334612,
0.03149585562972, 0.0319574876558628, 0.0335355316903225, 0.0328718821689198
)), class = "data.frame", row.names = c(NA, -10L))
#create and save graph as pdf
p1 <- ggplot(data=df, mapping=aes(y=Beta_Treatment, x=t)) +
geom_line(linetype="dashed", colour = "blue") +
geom_point(shape = 15) +
geom_errorbar(aes(ymin=(Beta_Treatment-1.96*treatment_stderr), ymax=(Beta_Treatment+1.96*treatment_stderr)), width=1) +
ylim(c(0,2.5)) + labs(y = "Beta") +
geom_vline(xintercept = 6) +
scale_x_continuous(limits = c(0, 11), breaks = c(0, 2, 4, 6, 8, 10)) +
theme_bw()
ggsave(last_plot(),filename = "plot.pdf")
p2 <- p1 + geom_line(aes(y=Beta_Control, x=t),linetype="dashed", colour = "green")+
geom_point(aes(y=Beta_Control, x=t), shape = 19) +
geom_errorbar(aes(ymin=(Beta_Control-1.96*control_stderr), ymax=(Beta_Control+1.96*control_stderr )), width=1) +
ylim(c(0,2.5)) +
geom_vline(xintercept = 6) +
theme_bw()
ggsave(last_plot(),filename = "plot2.pdf")
If you want a legend then you have to map on aesthetics, i.e. move color inside aes() and you will automatically get a legend. However, to set your desired colors in that case requires to set them via scale_color_manual.
To this end I map some "speaking" category labels on the color aes and assign colors and labels to these categories inside scale_color_manual. Additionally I used the limits argument to set the order of the categories:
ggplot(data = df, mapping = aes(y = Beta_Treatment, x = t)) +
geom_line(aes(colour = "treat"), linetype = "dashed") +
geom_point(shape = 15) +
geom_errorbar(aes(ymin = (Beta_Treatment - 1.96 * treatment_stderr),
ymax = (Beta_Treatment + 1.96 * treatment_stderr)), width = 1) +
ylim(c(0, 2.5)) +
labs(y = "Beta", color = NULL) +
geom_vline(xintercept = 6) +
scale_x_continuous(limits = c(0, 11), breaks = c(0, 2, 4, 6, 8, 10)) +
theme_bw() +
geom_line(aes(y = Beta_Control, color = "control"), linetype = "dashed") +
geom_point(aes(y = Beta_Control), shape = 19) +
geom_errorbar(aes(ymin = (Beta_Control - 1.96 * control_stderr),
ymax = (Beta_Control + 1.96 * control_stderr)), width = 1) +
scale_color_manual(values = c(treat = "blue", control = "green"),
labels = c(treat = "Treatment", control = "Control"),
limits = c("treat", "control"))
I have a bland-altman plot of 16 measurements divided over 3 groups (Slice) which I want to colorcode and possibly have different shapes but somehow I cant get it working:
df <- data.frame("Slice" = c(1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3),
"Segments" = c(1:16),
"mean" = c(6,5,2,4,8,9,6,3,5,6,5,8,5,4,6,4),
"dif" = c(1,3,2,1,2,3,2,1,2,2,2,1,3,2,1,2))
#creat limits of agreement
LL = mean(df$dif)-1.96*(sd(df$dif))
UL = mean(df$dif)+1.96*(sd(df$dif))
#create BA plot
BAplot <- ggplot(df, aes(x=mean,y=dif))+
geom_jitter(alpha=1.0,size=18,shape="*", stroke = 1.5)+
geom_hline(yintercept=mean(df$dif),color= "blue",size=2)+
geom_text(aes(x = 12, y = mean(df$dif)+0.2, label = round(mean(df$dif), 1)), col = "blue", size = 7) +
geom_hline(yintercept=0,linetype=3,size=2) +
geom_hline(yintercept=c(UL,LL),color="black",linetype="dashed",size=2)+theme_bw()+
geom_text(aes(x = 12, y = UL+0.2, label = round(UL,1)), col = "black", size = 7) +
geom_text(aes(x = 12, y = LL+0.2, label = round(LL,1)), col = "black", size = 7) +
scale_x_continuous("mean",limits = c(-2,12))+
scale_y_continuous("diff", limits = c(-1, 5.5))
To code your points by color and to have different shapes you have to map your Slice column on the color and/or shape aesthetic inside geom_jitter. As Slice is a numeric I first converted it to a factor. If you want specific colors or shape you could set your desired values using scale_color_manual and scale_shape_manual:
library(ggplot2)
ggplot(df, aes(x = mean, y = dif)) +
geom_jitter(aes(color = factor(Slice), shape = factor(Slice)), alpha = 1.0, size = 2) +
geom_hline(yintercept = mean(df$dif), color = "blue", size = 2) +
geom_text(aes(x = 12, y = mean(dif) + 0.2, label = round(mean(dif), 1)), col = "blue", size = 7) +
geom_hline(yintercept = 0, linetype = 3, size = 2) +
geom_hline(yintercept = c(UL, LL), color = "black", linetype = "dashed", size = 2) +
theme_bw() +
geom_text(aes(x = 12, y = UL + 0.2, label = round(UL, 1)), col = "black", size = 7) +
geom_text(aes(x = 12, y = LL + 0.2, label = round(LL, 1)), col = "black", size = 7) +
scale_x_continuous("mean", limits = c(-2, 12)) +
scale_y_continuous("diff", limits = c(-1, 5.5))
I made a plot for presenting OR ratio. However, OR for log(PTH) has a large 95CI. Can I make a gap into x axis between 20 and 30, making other variables more visible? Some, examples suggest gap_plot(), but I do not know how to combine it with this type of graph.
I already transform PTH value, so it will be hard to change it regarding interpretation. If you have some other way to do it, feel free to suggest. I would like to make values of all variables visible. However, this vales of lof(PTH) makes Pol hard to interpret from the plot, despite being significant.
Best,
A.
library(gridExtra)
library(ggplot2)
Nezavisna<-c("Pol","Starost","Ca","P","log(PTH)","log(mg)","BrojZlezda","MIBI","Iskustvo","Pridruzena")
OR<-c(0.399,1.023,0.814,0.568,14.14,0.417,2.193,0.709,1.468,1.445)
LL<-c(0.174,0.996,0.277,0.122,4.969,0.197,1.019,0.339,0.754,0.702)
UL<-c(0.917,1.052,2.396,2.649,40.238,0.882,4.722,1.483,2.859,2.976)
istrazivanje<-data.frame(Nezavisna,OR,LL,UL)
istrazivanje
ggplot(istrazivanje, aes(y = Nezavisna, x = OR)) +geom_point(shape = 18, size = 5) + geom_errorbarh(aes(xmin = LL, xmax = UL), height = 0.25) + geom_vline(xintercept = 1, color = "red", linetype = "dashed", cex = 1, alpha = 0.5)
As mentioned by stefan in the comments you can transform your data, but to make it readable set your labels and breaks to the normal values.
ggplot(istrazivanje, aes(y = Nezavisna, x = log2(OR))) +
geom_point(shape = 18, size = 5) +
geom_errorbarh(aes(xmin = log2(LL), xmax = log2(UL)), height = 0.25) +
geom_vline(xintercept = log2(1), color = "red", linetype = "dashed", cex = 1, alpha = 0.5) +
scale_x_continuous(breaks = log2(c(1, 10, 20, 30, 40)), labels =c(1, 10, 20, 30, 40))
Edit: or as you already take log(PTH) you could do log10(PTH) which roughly bring your OR value there from roughly 14 to 6 and perhaps then you do not even have to transform the scales of the graph.
If, I understand correctly your question one solution could be ggforce facet zoom function
Sample code:
library(gridExtra)
library(ggplot2)
library(ggforce)
ggplot(istrazivanje, aes(y = Nezavisna, x = OR)) +
geom_point(shape = 18, size = 5) +
geom_errorbarh(aes(xmin = LL, xmax = UL), height = 0.25) +
geom_vline(xintercept = 1,
color = "red",
linetype = "dashed",
cex = 1,
alpha = 0.5)+
facet_zoom(xlim = c(20, 30)) # use facet_zoom to zoom on x axis
Plot:
Sample data:
Nezavisna<-c("Pol","Starost","Ca","P","log(PTH)","log(mg)","BrojZlezda","MIBI","Iskustvo","Pridruzena")
OR<-c(0.399,1.023,0.814,0.568,14.14,0.417,2.193,0.709,1.468,1.445)
LL<-c(0.174,0.996,0.277,0.122,4.969,0.197,1.019,0.339,0.754,0.702)
UL<-c(0.917,1.052,2.396,2.649,40.238,0.882,4.722,1.483,2.859,2.976)
istrazivanje<-data.frame(Nezavisna,OR,LL,UL)
istrazivanje
Here is the data set:
d <- tribble(
~priceseg, ~price_n, ~zet_n, ~zet_n2,
"(0,1]", 16, 2, 24,
"(1,3]", 33, 3, 38,
"(3,5]", 33, 2, 25,
"(5,6]", 17, 1, 13,
)
And here is the visualisation thanks to #d.b
ggplot(d) +
geom_col(aes(x = priceseg, y = price_n), fill = ("#F1948A"), colour="black", size = 0.6) +
geom_line(data = d, mapping = aes(x = priceseg, y = zet_n2, group = 1), colour = "#154360", size = 1) +
geom_label(data = d, mapping = aes(x = priceseg, y = price_n, label = price_n), nudge_y = -0.6)
Now, I want to add the legend for bar plot and line in the visualisation something like this: Combined line & bar geoms: How to generate proper legend?
Also, I would like to add % in geom_label.
But somehow, I could not manage to implement it. Any help?
Here is an option
# Calculate percentage and add as column to `d`
d <- transform(d, perc = sprintf("%2.1f%%", price_n / sum(price_n) * 100))
# Plot
ggplot(d, aes(x = priceseg)) +
geom_col(aes(y = price_n, fill = "bar_data"), colour = "black", size = 0.6) +
geom_line(aes(y = zet_n2, group = 1, colour = "line_data"), size = 1) +
scale_fill_manual("", values = "#F1948A") +
scale_colour_manual("", values = "#154360") +
geom_label(aes(y = price_n, label = perc), nudge_y = -0.6) +
theme(
legend.key = element_blank(),
legend.title = element_blank(),
legend.box = "horizontal")
You can adjust the fill and colour "labels" by changing the strings "bar_data" and "line_data".