I have a dataframe like this
library(ggplot2)
df = data.frame(N = c(12, 18))
And I want to get
I do
ggplot()+ geom_segment(aes(x = 0, y = 0.5, xend = max(df$N), yend =0.5), color="grey50",
linetype="dashed", size=1.5) +
geom_segment(aes(x = df$N[1], y = 0, xend = df$N[1], yend = 0.5), color="grey50",
linetype="dashed", size=1.5)+
geom_segment(aes(x = df$N[2], y = 0, xend = df$N[2], yend = 0.5), color="grey50",
linetype="dashed", size=1.5)
The problem is that the data can change, the rows can be not two, but three or more, and the code becomes incorrect. So I try to use a loop
v = list()
for (i in 1:length(df$N)) {
n = geom_segment(aes(x = df$N[i], y = 0, xend = df$N[i], yend = 0.5), color="grey50",
linetype="dashed", size=1.5)
v = append(n, v)
}
v
ggplot()+ geom_segment(aes(x = 0, y = 0.5, xend = max(df$N), yend =0.5), color="grey50",
linetype="dashed", size=1.5) + v
But the diagram only shows the last line. How to fix the loop or I have to do another way?
You don't need a loop; geom_segment can handle multiple inputs from a vector.
df1 <- data.frame(N = c(12, 18, 24))
library(ggplot2)
ggplot(data = df1)+
geom_segment(aes(x = 0, y = 0.5, xend = max(N), yend =0.5),
color="grey50", linetype="dashed", size=1.5) +
geom_segment(aes(x = N, y = 0, xend = N, yend = 0.5),
color="grey50", linetype="dashed", size=1.5)
Related
I would like to create a shaded area in color blue between the two dotted lines (-0.5 and 0.5), tried with geom_polygon() but didn't work.
How can this be done in the best possible way?
model <- lm(Sepal.Width ~ Petal.Length, data = iris)
ggplot(data.frame(x = seq(model$residuals), y = model$residuals)) +
geom_point(aes(x, y)) +
geom_hline(yintercept = 0, linetype = "dashed") +
geom_hline(yintercept = 0.5, linetype = "dotted") +
geom_hline(yintercept = -0.5, linetype = "dotted") +
labs(x = "Index", y = "Residuals",
title = paste("Residuals of", format(model$call)))
You can use geom_ribbon
ggplot(data.frame(x = seq(model$residuals), y = model$residuals)) +
geom_point(aes(x, y)) +
geom_ribbon(aes(x, ymin = -0.5, ymax = 0.5), alpha = 0.3, fill = 'steelblue')+
geom_hline(yintercept = 0, linetype = "dashed") +
geom_hline(yintercept = 0.5, linetype = "dotted") +
geom_hline(yintercept = -0.5, linetype = "dotted") +
labs(x = "Index", y = "Residuals",
title = paste("Residuals of", format(model$call)))
With annotate:
annotate("rect", xmin = -Inf, xmax = Inf, ymin = -0.5, ymax = 0.5, alpha = .2, fill = "blue")
Output:
ggplot(data.frame(x = seq(model$residuals), y = model$residuals)) +
geom_point(aes(x, y)) +
geom_hline(yintercept = 0, linetype = "dashed") +
geom_hline(yintercept = c(-0.5, 0.5), linetype = "dotted") +
annotate("rect", xmin = -Inf, xmax = Inf, ymin = -0.5, ymax = 0.5, alpha = .2, fill = "blue") +
labs(x = "Index", y = "Residuals",
title = paste("Residuals of", format(model$call)))
Is there a way to create a figure similar to the one below without having any data on this?
You could do something like this. You could add another geom_curve and a couple of geom_vlines.
library(tidyverse)
ggplot() +
geom_abline() +
geom_curve(aes(x = 0, y = 0, xend = 1, yend = 1), curvature = -0.4) +
annotate("text", x = 0.5, y = 0.5, label = "Line of Equality", angle = 35, vjust = 2) +
labs(x = "Individuals Neighbourhoods\nAcross Space", y = "Scoioeconomic Position") +
theme_minimal() +
theme(axis.text = element_blank())
Created on 2022-04-27 by the reprex package (v2.0.1)
For the arrows you can use this code: arrow = arrow(length = unit(0.5, "cm")) in a geom_segment. It is a bit tricky without any numbers, but maybe you want something like this:
library(ggplot2)
ggplot() +
geom_abline(slope = 1) +
geom_curve(aes(x = 0, y = 0, xend = 1, yend = 1), curvature = -0.4) +
geom_curve(aes(x = 0, y = 1, xend = 1, yend = 0.1), curvature = 0.4, linetype = "dashed") +
geom_segment(aes(x=0.9,y=0.98,xend=0.9,yend=0.12), arrow = arrow(length = unit(0.5, "cm"))) +
geom_segment(aes(x=0.02,y=0.85,xend=0.02,yend=0.23), arrow = arrow(length = unit(0.5, "cm"))) +
annotate("text", x = 0.5, y = 0.5, label = "Line of Equality", angle = 45, vjust = 2) +
annotate("text", x = 0.25, y = 0.75, label = "Income (etc.)", angle = 45, vjust = 2) +
annotate("text", x = 1.2, y = 0.1, label = "Corresponding\nExposure", angle = 0) +
labs(x = "Individuals Neighbourhoods\nAcross Space", y = "Scoioeconomic Position") +
scale_x_continuous(limits = c(0, 1.3)) +
theme_minimal() +
theme(axis.text = element_blank())
Output:
i have following Chart:
Author_csv %>%
ggplot(aes(x=X, y=Y)) +
geom_jitter(aes(color=Autor), show.legend = F) +
geom_segment(aes(x = 0, xend = 7, y = 0, yend = 3.5, colour = "dashed"), linetype="dashed", size=0.3) +
geom_segment(aes(x = 0, xend = 7, y = 7, yend = 3.5, colour = "dashed"), linetype="dashed", size=0.3) +
geom_segment(aes(x = 0, xend = 7, y = 0, yend = 7, colour = "solid"), linetype="solid", size=0.3)
resulting in this graph:
I want to suppress the Jitter from the Legend. I only want to see my geom segment Lines with the label dashed/solid. How would i do this?
Instead of
geom_jitter(aes(color=Autor), show.legend = F)
Try using
geom_jitter(aes(fill=Autor), shape = 21, colour = "transparent", show.legend = FALSE)
Here is an example using the palmerpenguins dataset:
library(tidyverse)
library(palmerpenguins)
penguins %>%
na.omit() %>%
ggplot(aes(x=species, y=bill_length_mm)) +
geom_jitter(aes(fill=island), shape = 21, colour = "transparent", show.legend = FALSE) +
geom_segment(aes(x = 0, xend = 3.5, y = 0, yend = 35, colour = "dashed"), linetype="dashed", size=0.3) +
geom_segment(aes(x = 0, xend = 3.5, y = 70, yend = 35, colour = "dashed"), linetype="dashed", size=0.3) +
geom_segment(aes(x = 0, xend = 3.5, y = 0, yend = 70, colour = "solid"), linetype="solid", size=0.3)
Created on 2021-09-23 by the reprex package (v2.0.1)
I would really appreciate it if anyone can help me use the thickness of the lines as a legend.
Thin line = low correlation
Think line = High correlation
I tried using the size of the dots to show the thickness but it confuses the audience even further. I am open to other creative methods to communicate this message to the audience.
My code is listed below.
Thanks in advance!
library(ggplot2)
# custom empty theme to clear the plot area
empty_theme <- theme(
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text.y = element_text(angle = 90)
)
plot <- ggplot(NULL, aes()) +
# fix the scale so it's always a square
coord_fixed() +
# set the scale to one greater than 0-10 in each direction
# this gives us some breating room and space to add some arrows
scale_x_continuous(expand = c(0, 0), limits = c(-1, 11),
breaks = c(2,8), labels=c("2" = "", "8" = "")) +
scale_y_continuous(expand = c(0, 0), limits = c(-1,11),
breaks = c(2,8), labels=c("2" = "", "8" = "")) +
# apply the empty theme
empty_theme +
# labels
labs(title = "Magic Quadrant",
x = "Completeness of Vision",
y = "Ability to Execute") +
# create the quadrants
geom_segment(aes(x = 10, y = 0, xend = 10, yend = 10), color = "#EDEDED") +
geom_segment(aes(x = 0, y = 0, xend = 0, yend = 10), color = "#EDEDED") +
geom_segment(aes(x = 0, y = 0, xend = 10, yend = 0), color = "#EDEDED") +
geom_segment(aes(x = 0, y = 5, xend = 10, yend = 5), color = "#EDEDED") +
geom_segment(aes(x = 5, y = 0, xend = 5, yend = 10), color = "#EDEDED") +
geom_segment(aes(x = 0, y = 10, xend = 10, yend = 10), color = "#EDEDED") +
#Rectangle
geom_rect(mapping = aes(xmin = 0, xmax = 5, ymin = 0, ymax = 5), fill = "#deecff") +
geom_rect(mapping = aes(xmin = 5, xmax = 10, ymin = 5, ymax = 10), fill = "#deecff") +
# quadrant labels
annotate("text", x = 2.5, y = 2.5, alpha = 0.35, label = "Niche Players", color = "#979b9c") +
annotate("text", x = 2.5, y = 7.5, alpha = 0.35, label = "Challengers", color = "#979b9c") +
annotate("text", x = 7.5, y = 2.5, alpha = 0.35, label = "Visionaries", color = "#979b9c") +
annotate("text", x = 7.5, y = 7.5, alpha = 0.35, label = "Leaders", color = "#979b9c") +
# arrows are cut in half which conveniently matches the gartner one
annotate("segment", x = 0, xend = 10, y = -1, yend = -1,colour = "blue",
size=2, alpha=1, arrow=arrow(type = "closed", angle = 15)) +
annotate("segment", x = -1, xend = -1, y = 0, yend = 10, colour = "blue",
size=2, alpha=1, arrow=arrow(type = "closed", angle = 15))
tools_quad_data <- data.frame(
title = c("A", "B", "C", "D", "E"),
value = c(6,6,6.3,6,8),
effort = c(3,8,9,4,7.9),
txt_position_value = c(6,6,6,6.2,8),
txt_position_effort = c(3.3,8.3,9.3,4.3,8.2)
)
plot <- plot +
geom_point(data = tools_quad_data, aes(x = value, y = effort, color = "ML Tool"), size = 4) +
geom_text(data = tools_quad_data, aes(label = title, x = txt_position_value, y = txt_position_effort), color = "#011d80")
db_quad_data <- data.frame(
title = c("U","V", "W","X","Y","Z"),
value = c(6.5,9,7.3,8,2,1),
effort = c(2.2, 9, 1, 3.4,1.5,0.1),
txt_position_value = c(6.7, 9.2, 7,8,2.2,1.3),
txt_position_effort = c(2,9.3, 0.7,3.1,1.3, 0.35)
)
plot <- plot +
geom_point(data = db_quad_data, aes(x = value, y = effort, color = "Database"), size = 4) +
geom_text(data = db_quad_data, aes(label = title, x = txt_position_value, y = txt_position_effort ), color = "#e09900") +
scale_colour_manual(name="Legend", values=c(Database="#e09900", `ML Tool`="#011d80"))
plot +
geom_curve(aes(x = 6.3, y = 9, xend = 2, yend = 1.5),
color = "black", curvature = 0.2, size = 0.5, alpha = 0.1) +
geom_curve(aes(x = 8, y = 7.9, xend = 2, yend = 1.5),
color = "black", curvature = -0.2, size = 1.5, alpha = 0.1) +
geom_curve(aes(x = 6, y = 4, xend = 2, yend = 1.5),
color = "black", curvature = -0.1, size = 1, alpha = 0.1) +
geom_curve(aes(x = 6, y = 8,
xend = 6.5, yend = 2.2),
color = "black", curvature = -0.1, size = 2, alpha = 0.1) +
geom_curve(aes(x = 8, y = 7.9,
xend = 6.5, yend = 2.2),
color = "black", curvature = -0.1, size = 0.8, alpha = 0.1) +
geom_curve(aes(x = 8, y = 3.4,
xend = 8, yend = 7.9),
color = "black", curvature = 0.1, size = 0.6, alpha = 0.1) +
geom_curve(aes(x = 9, y = 9,
xend = 6, yend = 4),
color = "black", curvature = -0.3, size = 1.5, alpha = 0.1) +
geom_curve(aes(x = 9, y = 9,
xend = 6, yend = 8),
color = "black", curvature = 0.3, size = 1.3, alpha = 0.1) +
geom_curve(aes(x = 7.3, y = 1,
xend = 6, yend = 8),
color = "black", curvature = 0.3, size = 0.5, alpha = 0.1)
Another option is to use different linetypes, these are generally more distinguishable than line thickness. This can be controlled with the linetype aesthetic
I want to highlight the area between a vertical line and a normal distributed function. I know how it works with discrete values, but the stat_function confuses me. The code looks something like this:
library(ggplot2)
n1 <- 5
ggplot(data.frame(x = c(-2, 2)), aes(x)) +
stat_function(fun = dnorm, args = list(sd = 1/sqrt(n1))) +
geom_vline(xintercept = 0.5, linetype = "dashed", color = "red", size = 1) +
geom_vline(xintercept = -0.5, linetype = "dashed", color = "red", size = 1) +
ylim(c(0, 1.5)) +
theme_light() +
geom_rect(aes(xmin = 0.5, xmax = Inf, ymax = Inf, ymin = 0), fill = "grey", alpha = .3)
I know I need to change ymax to the values of x > 0.5. The question is how?
EDIT:
I looked into the question which is supposed to be the same as mine. When I rewrite the code the way they did, the highlighting works but it doesn't give me a proper normal distribution anymore, as you can see here:
library(dplyr)
set.seed(123)
range <- seq(from = -2, to = 2, by = .01)
norm <- rnorm(range, sd = 1 / sqrt(n1))
df <- data_frame(x = density(norm)$x, y = density(norm)$y)
ggplot(data_frame(values = norm)) +
stat_density(aes(x = values), geom = "line") +
geom_vline(xintercept = 0.5, linetype = "dashed", color = "red", size = 1) +
geom_vline(xintercept = -0.5, linetype = "dashed", color = "red", size = 1) +
ylim(c(0, 1.5)) +
theme_light() +
geom_ribbon(data = filter(df, x > 0.5),
aes(x = x, ymax = y), ymin = 0, fill = "red", alpha = .5)
When I stick with stat_function and use geom_ribbon with subsetting as proposed in the very same question, it highlights buggy, as you can see here:
ggplot(data_frame(x = c(-2, 2)), aes(x)) +
stat_function(fun = dnorm, args = list(sd = 1/sqrt(n1))) +
geom_vline(xintercept = 0.5, linetype = "dashed", color = "red", size = 1) +
geom_vline(xintercept = -0.5, linetype = "dashed", color = "red", size = 1) +
ylim(c(0, 1.5)) +
theme_light() +
geom_ribbon(data = filter(df, x > 0.5),
aes(x = x, ymax = y), ymin = 0, fill = "red", alpha = .5)
Not satisfying yet.
Here is an approach:
library(ggplot2)
n1 <- 5
ggplot(data.frame(x = c(-2, 2)), aes(x)) +
stat_function(fun = dnorm, geom = "area", fill = "grey", alpha = 0.3, args = list(sd = 1/sqrt(n1)), xlim = c(-0.5,0.5)) +
stat_function(fun = dnorm, args = list(sd = 1/sqrt(n1))) +
geom_vline(xintercept = 0.5, linetype = "dashed", color = "red", size = 1) +
geom_vline(xintercept = -0.5, linetype = "dashed", color = "red", size = 1) +
ylim(c(0, 1.5)) +
theme_light()
in stat_function one can define different geom, just pick the ones that suits your needs.