How do I graph on custom plots in ggplot2? [closed] - r

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am using a plot obtained from the following site:
https://github.com/statsbylopez/blogposts/blob/master/fball_field.R
I do not know how to plot points on this. How would I go about doing this?

«...and thus haven't tried anything.» — Well, then just do that: try! You cannot fail!
base.football +
geom_point(aes(1:10, seq(1, 100, 10)), color = "red") +
geom_point(aes(0, 50), color = "blue")
Data
library(ggplot2)
theme.football <- function(){
theme(panel.grid.minor = element_blank(),
axis.text.y = element_text(angle=270, hjust=0.5),
panel.border = element_blank(),
panel.grid.major = element_line(size = 0.5, linetype = 'solid',
colour = "black"),
axis.ticks = element_blank())
}
xlim <- (160/3)/2
hash.width <- 3.3
hash.x <- (xlim + hash.width)/2
df.hash <- expand.grid(x = c(-1*xlim, -1*hash.width, hash.width, xlim), y = (0:100))
df.hash <- df.hash %>% filter(!(floor(y %% 5) == 0))
base.football <- ggplot() + xlab("") + ylab("") +
theme_minimal() +
annotate("segment", x = c(-1*xlim, -1*xlim, xlim, xlim),
y = c(-10, 110, 110, -10),
xend = c(-1*xlim, xlim, xlim, -1*xlim),
yend = c(110, 110, -10, -10), colour = "black") +
#geom_point(data = df.hash, aes(x, y), pch = 1) +
annotate("text", x = df.hash$x[df.hash$x < 0], y = df.hash$y[df.hash$x < 0], label = "_", hjust = 0, vjust = -0.2) +
annotate("text", x = df.hash$x[df.hash$x > 0], y = df.hash$y[df.hash$x > 0], label = "_", hjust = 1, vjust = -0.2) +
annotate("segment", x = rep(-1*xlim, 21),
y = seq(0, 100, by = 5),
xend = rep(xlim, 21),
yend = seq(0, 100, by = 5)) +
annotate("text", x = rep(-1*hash.x, 11), y = seq(0, 100, by = 10),
label = c("G ", seq(10, 50, by = 10), rev(seq(10, 40, by = 10)), " G"),
angle = 270, size = 4) +
annotate("text", x = rep(hash.x, 11), y = seq(0, 100, by = 10),
label = c(" G", seq(10, 50, by = 10), rev(seq(10, 40, by = 10)), "G "),
angle = 90, size = 4) +
scale_y_continuous("", breaks = NULL, lim = c(-10, 110)) +
scale_x_continuous("", breaks = NULL, lim = c(-1*xlim, xlim)) +
annotate("rect", xmin=-1*xlim, xmax=xlim, ymin=-10, ymax=110, fill="palegreen", alpha=0.1) +
theme.football()

Related

How to apply slope plot R code to another data

I have dataframe which represents sales by model within 2 different years. 'change' column stands for absolute change by models from 2020 to 2021 while 'chng.percent' measures this change in percentages.
However, I am struggling to apply the given Code of slope plot to my data.
df <- data.frame (model = c("A", "A", "B","B"),
year = c(2020,2021,2020,2021),
sale =c(105,190,110,180),
chang = c(85,NA,70,NA),
chng.percent = c(80.9,NA, 63.6,NA))
Expected outcome (Like this)
Here's a way to do it all within ggplot using your existing data:
ggplot(df, aes(year, sale, color = model)) +
geom_line(arrow = arrow(type = "closed", angle = 20),
key_glyph = draw_key_point) +
geom_vline(aes(xintercept = year)) +
geom_text(aes(label = sale, hjust = ifelse(year == 2020, 1.3, -0.3)),
color = "black",
size = 6) +
geom_text(aes(x = min(df$year) + 0.25, y = 105,
label = paste0("+", chang[1], "; ", chng.percent[1], "%"),
color = "A"), size = 5) +
geom_text(aes(x = max(df$year) - 0.25, y = 150,
label = paste0("+", chang[3], "; ", chng.percent[3], "%"),
color = "B"), size = 5) +
theme_void(base_size = 16) +
coord_cartesian(clip = "off") +
scale_x_continuous(breaks = c(2020, 2021)) +
guides(color = guide_legend(override.aes = list(size = 5))) +
scale_color_brewer(palette = "Set1") +
theme(plot.margin = margin(30, 30, 30, 30),
aspect.ratio = 1.5,
axis.text.x = element_text(size = 20))
you can try something like this :
df <- data.frame(model = c("A", "B"),
sale_2020 =c(105,110),
sale_2021 =c(190,180),
chang = c(85,70),
chng.percent = c(80.9, 63.6))
df %>%
ggplot() +
geom_segment(aes(x = 1, xend = 2,
y = sale_2020,
yend = sale_2021,
group = model,
col = model),
size = 1.2) +
# set the colors
scale_color_manual(values = c("#468189", "#9DBEBB"), guide = "none") +
# remove all axis stuff
theme_classic() +
theme(axis.line = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank()) +
geom_text(aes(x = x, y = y, label = label),
data = data.frame(x = 1:2,
y = 10 + max(df$sale_2021),
label = c("2020", "2021")),
col = "grey30",
size = 6) +
# add vertical lines that act as axis for 2020
geom_segment(x = 1, xend = 1,
y = min(df$sale_2020) -10,
yend = max(df$sale_2020) + 81,
col = "grey70", size = 1.5) +
# add vertical lines that act as axis for 2021
geom_segment(x = 2, xend = 2,
y = min(df$sale_2021) - 80,
yend = max(df$sale_2021) + 1,
col = "grey70", size = 1.5) +
# add the success rate next to each point on 2021 axis
geom_text(aes(x = 2 + 0.08,
y = sale_2021,
label = paste0(round(sale_2021, 1))),
col = "grey30") +
# add the success rate next to each point on 2021 axis
geom_text(aes(x = 1 - 0.08,
y = sale_2020,
label = paste0(round(sale_2020, 1))),
col = "grey30") +
# add the success rate next to each point on 2020 axis
geom_text(aes(x = 2 - 0.5,
y = c(156, 135),
label = paste0(round(chng.percent, 1), "%")),
col = "grey30")

Create a special Radial bar chart (race track plot)

I was able to replicate another good answers here to create a basic radial plot, but can anyone give me any clue of others functions/parameters/ideas on how to convert the basic one to something similar to this :
You could get pretty close like this:
df <- data.frame(x = c(10, 12.5, 15), y = c(1:3),
col = c("#fcfbfc", "#fbc3a0", "#ec6f4a"))
library(ggplot2)
ggplot(df, aes(x = 0, xend = x, y = y, yend = y, color = col)) +
geom_hline(yintercept = c(1:3), size = 14, color = "#dfdfdf") +
geom_hline(yintercept = c(1:3), size = 13, color = "#f7f7f7") +
geom_segment(color = "#bf2c23", size = 14, lineend = 'round') +
geom_segment(size = 13, lineend = 'round') +
scale_color_identity() +
geom_point(aes(x = x - 0.03 * y), size = 5, color = "#bf2c23",
shape = 21, fill = 'white') +
geom_point(aes(x = x - 0.03 * y), size = 2, color = "#bf2c23",
shape = 21, fill = 'white') +
scale_y_continuous(limits = c(0, 4)) +
scale_x_continuous(limits = c(0, 20)) +
coord_polar() +
theme_void()
Here's a start. Are there particular aspects you're trying to replicate? This is a fairly customized format.
df <- data.frame(type = c("on", "ia", "n"),
radius = c(2,3,4),
value = c(10,21,22))
library(ggplot2); library(ggforce)
ggplot(df) +
geom_link(aes(x = radius, xend = radius,
y = 0, yend = value),
size = 17, lineend = "round", color = "#bb353c") +
geom_link(aes(x = radius, xend = radius,
y = 0, yend = value, color = type),
size = 16, lineend = "round") +
geom_label(aes(radius, y = 30,
label = paste(type, ": ", value)), hjust = 1.8) +
scale_x_continuous(limits = c(0,4)) +
scale_y_continuous(limits = c(0, 30)) +
scale_color_manual(values = c("on" = "#fff7f2",
"ia" = "#f8b68f",
"n" = "#e4593a")) +
guides(color = "none") +
coord_polar(theta = "y") +
theme_void()

I am not able to see x-y-axis labels and annotation_logticks in my graph, any suggestion?

Updated code, i am trying to figure out with both x-axis and y-axis do not appear. I also want y-axis in log scale, and x-axis is natural scale. I tried to clean up variables but still does not work. Any suggestion?
This is how my code looks like:
r_blade_mp_25 <-c(0.9996, 1, 1, 1, 0.999945, 0.99988, 0.9996)
p_blade_mp_25 <- c(1559.18, 1410.15, 1492.67, 1439.17, 1518.26, 1533.52, 1559.18)
r_blade_mp_50 <-c(0.999578, 1, 1, 1, 0.999942, 0.999884, 0.999578)
p_blade_mp_50 <- c(1558.91, 1331.39, 1492.01, 1418.74, 1517.34, 1533.35, 1558.91)
r_blade_mp_75 <-c(0.999573, 0.999699, 0.999893, 0.999929, 0.999896, 0.999835, 0.999573)
p_blade_mp_75 <- c(1544.27, 1036.09, 1436.59, 1197.41, 1465.64, 1521.52, 1544.27)
x_blade_mp_25 <- c(25, 25, 25, 25, 25, 25, 25)
x_blade_mp_50 <- c(50, 50, 50, 50, 50, 50, 50)
x_blade_mp_75 <- c(75, 75, 75, 75, 75, 75, 75)
df_blade_mp_25 <- data.frame(x_blade_mp_25, r_blade_mp_25)
df_blade_mp_50 <- data.frame(x_blade_mp_50, r_blade_mp_50)
df_blade_mp_75 <- data.frame(x_blade_mp_75, r_blade_mp_75)
df_blade_mp_25_power <- data.frame(x_blade_mp_25, p_blade_mp_25)
df_blade_mp_50_power <- data.frame(x_blade_mp_50, p_blade_mp_50)
df_blade_mp_75_power <- data.frame(x_blade_mp_75, p_blade_mp_75)
thresholds_reliability <- ggplot() +
theme_bw() +
theme(plot.title = element_text(size = 12, face = "bold", hjust = 0.5)) +
geom_point(data=df_blade_mp_25, aes(x=x_blade_mp_25, y=1-r_blade_mp_25, color = "#2fdac6"), size = 3) +
geom_point(data=df_blade_mp_50, aes(x=x_blade_mp_50, y=1-r_blade_mp_50, color = "#bb8fce"), size = 3) +
geom_point(data=df_blade_mp_75, aes(x=x_blade_mp_75, y=1-r_blade_mp_75, color = "#e38b27"), size = 3) +
scale_y_log10(labels = scales::math_format(format = log10), oob = scales::squish_infinite) +
#scale_y_continuous(trans='log10') +
labs(x = "Threshold aggressiveness",
y = bquote("Failure Probability" ~ (P[f])),
title = "Google Traces",
color = "Deployments") +
scale_color_manual(values = c("#2fdac6", "#bb8fce", "#e38b27"),
labels = c("25%", "50%", "75%"))
thresholds_reliability
#thresholds power consumption at 25%, 50%, 75%, 90%
thresholds_power <- ggplot() +
theme_bw() +
theme(plot.title = element_text(size = 12, face = "bold", hjust = 0.5)) +
geom_point(data=df_blade_mp_25_power, aes(x=x_blade_mp_25, y=p_blade_mp_25, color = "#2fdac6"), size = 3) +
geom_point(data=df_blade_mp_50_power, aes(x=x_blade_mp_50, y=p_blade_mp_50, color = "#bb8fce"), size = 3) +
geom_point(data=df_blade_mp_75_power, aes(x=x_blade_mp_75, y=p_blade_mp_75, color = "#e38b27"), size = 3) +
scale_y_log10(labels = scales::math_format(format = log10), oob = scales::squish_infinite) +
labs(x = "Threshold aggressiveness",
y = bquote("Failure Probability" ~ (P[f])),
title = "Google Traces",
color = "Deployments") +
scale_color_manual(values = c("#2fdac6", "#bb8fce", "#e38b27"),
labels = c("25%", "50%", "75%"))
thresholds_power
I have modified the script found below like this
lifetime_blade_mp <- ggplot() +
theme_bw() +
theme(plot.title = element_text(size = 12, face = "bold", hjust = 0.5)) +
geom_point(data=df_trace_1, aes(x=lifetime_blade_mp, y=1-trace_1, color = "#28b463"), size = 3) +
geom_point(data=df_trace_5, aes(x=lifetime_blade_mp, y=1-trace_5, color = "#e74c3c"), size = 3) +
geom_point(data=df_trace_6, aes(x=lifetime_blade_mp, y=1-trace_6, color = "#f4d03f"), size = 3) +
scale_y_log10(labels = scales::math_format(format = log10)) +
labs(x = "Lifetimes of servers (days)")+
labs(y = bquote("Failure Probability" ~ (P[f])))+
labs(title = "Google Cluster Data 50% thresholds", color = "Deployments") +
scale_color_manual(values = c("#28b463", "#e74c3c", "#8e44ad"),
labels = c("Google Trace #1", "Google Trace #2",
"Google Trace #3"))
It is working fine.

Annotate ggplot2 across both axis: text keeps changing position

I am trying to annotate both axes of my plot with some text, but when I do that, I am unable to position the text as I would like. By adding new text on one axis, the text on the other axis gets misplaced.
How to deal with that?
Here is an example to illustrate my issue:
set.seed(1234)
x <- rnorm(50, 5, 2)
y <- x + 1 + rnorm(50)
data <- cbind.data.frame(x,y)
#Create a plot in which I annotate in one axis (it works great)
plot <- ggplot(data = data, aes(x, y))+
geom_point() +
geom_hline(yintercept=median(data$x, na.rm = T), color = 'red') +
geom_vline(xintercept=median(data$y, na.rm = T), color = 'red') +
labs(y="Label y", x = "Label x") +
geom_smooth(method=lm, na.rm = TRUE, fullrange= TRUE,
aes(group=1),colour="black") +
theme_bw() +
theme(axis.title.y = element_text(margin = margin(t = 0, r = 30, b = 0, l = 0))) +
theme(axis.title.x = element_text(margin = margin(t = 30, r = 0, b = 0, l = 0))) +
annotate("text", x = 9, y = -3, label = "Helpful Text2") +
annotate("text", x = 0.5, y = -3, label = "Helpful Text1") +
coord_cartesian(ylim = c(0, 15), clip = "off")
#Trying to add annotation to the second axis (it alters the axis of the plot, thereby misplacing the annotation I have done prior)
plot + annotate("text", x = 0, y = 8.5, label = "Helpful Text3", angle = 90) +
annotate("text", x = 0, y = 2, label = "Helpful Text4", angle = 90) +
coord_cartesian(xlim = c(1, 9), clip = "off")
Ideas?
You could try:
plot + annotate("text", x = -1, y = 14, label = "Helpful Text3", angle = 90) +
annotate("text", x = -1, y = 0, label = "Helpful Text4", angle = 90) +
coord_cartesian(ylim = c(0, 15), xlim = c(0, 10), clip = "off")
Just make sure you set fullrange = FALSE in geom_smooth when defining your plot.

Spicing Up Native Circular Plot Using ggplot2 [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
CODE UPDATED
I have some angle data from an animal behavior study that I would like to plot for publication using ggplot2. What follows is my current workflow with some example data and how it would look using the generic plot function.
### Create two data frames of random Cartesian coordinates ###
df1 <- data.frame(
x = sample(10, 11, replace = TRUE),
y = sample(10, 11, replace = TRUE))
df2 <- data.frame(
x = sample(10, 11, replace = TRUE),
y = sample(10, 11, replace = TRUE))
### Write a function that converts continuous Cartesian coordinates to velocities ###
get.polar <- function(df)
{
x <- diff(df$x)
y <- diff(df$y)
d <- complex(real = x, imaginary = y)
steps <- data.frame(speed = Mod(d), angle = Arg(d))
steps[-1,] # Deletes the first row as it does not contain an angle measurement
steps$time <- (1:nrow(steps))/30 # generates a time column in seconds (1 data point = 1/30 of a second)
return(steps)
}
df1_polar <- get.polar(df1)
df2_polar <- get.polar(df2)
require(circular)
### Convert angles into an object of type 'circular' ###
df1_rad <- circular(df1_polar$angle, type = 'angles', units = 'radians', zero=0, rotation = "counter")
df2_rad <- circular(df2_polar$angle, type = 'angles', units = 'radians', zero=0, rotation = "counter")
### Convert radians to degrees with a clockwise rotation and zero at "north" ###
df1_deg <- conversion.circular(df1_rad, type = "angles", units = "degrees", zero = pi/2, rotation = "clock")
df2_deg <- conversion.circular(df2_rad, type = "angles", units = "degrees", zero = pi/2, rotation = "clock")
### Convert negative rotations to positive ###
df1_deg[df1_deg < 0] <- df1_deg[df1_deg < 0] + 360
df2_deg[df2_deg < 0] <- df2_deg[df2_deg < 0] + 360
par(pty = "s")
plot(df1_deg, units = "degrees")
ticks.circular(circular(seq(0,(11/6)*pi, pi/6)), zero = pi/2, rotation = "clock", tcl = 0.075)
points(df2_deg, zero = pi/2, rotation = "clock", pch = 16, col = "darkgrey", next.points = -0.2)
# Suggested solution by MLavoie with modifications
temp1 <- data.frame(Exercise = c(1, 1, 1, 1), Name = c(1, 2, 3, 4),
Score = c(90, 180, 270, 360))
temp2 <- data.frame(Name=c(replicate(length(df1_deg), 3)),
Score = c(df1_deg))
temp3 <- data.frame(Name=c(replicate(length(df2_deg), 4)),
Score = c(df2_deg))
temp4 <- data.frame(Name=c(4.8, 4.8, 4.8, 4.8, 4.8, 4.8, 4.8, 4.8),
Score = c(0, 45, 90, 135, 180, 225, 270, 315))
ggplot() +
geom_bar(data = temp1, aes(x = factor(Name), y = Score, fill = factor(Exercise)),
width = 1, stat = 'identity') +
geom_point(data = temp2, aes(x = Name, y = Score),
color = "green", size = 2) +
geom_point(data = temp3, aes(x = Name, y = Score),
color = "red", size = 2) +
geom_point(data = temp4, aes(x = Name, y = Score),
color = "black", shape = 8, size = 2) +
geom_vline(xintercept = 4.8) +
annotate("text", x = 0, y = 0, label = "+", size = 6) +
scale_y_continuous(breaks = c(0, 45, 90, 135, 180, 225, 270, 315)) +
coord_polar(theta = "y", start = 0) +
theme_bw() + ylab("") + xlab("") +
theme(panel.border = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
strip.text = element_blank(),
strip.background = element_blank(),
axis.text.y = element_blank(),
legend.position = "none",
axis.ticks = element_blank()) +
scale_fill_manual(values = c("transparent", "transparent", "transparent", "transparent"))
Some suggestions for turning this rough plot into something publishable using ggplot2?
Thank you!
What about this for a start:
temp <- data.frame(Exercise=c(1, 1, 1, 1), Name=c(1, 2, 3, 4), Score=c(90, 180, 270, 360))
temp2 <- data.frame(Name=c(2.8, 2.8, 2.8, 2.8), Score=c(90, 180, 270, 360))
temp3 <- data.frame(Name=c(4.2, 4.2, 4.2, 4.2), Score=c(90, 180, 270, 360))
temp4 <- data.frame(Name=c(0), Score=c(180))
temp5 <- data.frame(Name=c(4.8, 4.8, 4.8, 4.8, 4.8, 4.8, 4.8, 4.8), Score=c(45, 90, 135, 180, 225, 270, 305, 360))
ggplot() +
geom_bar(data=temp, aes(x = factor(Name), y=Score, fill = factor(Exercise)), width = 1, stat='identity') +
geom_point(data=temp2, aes(x=Name, y=Score), color="grey") +
coord_polar(theta = "y", start=0) +
theme_bw() + ylab("") + xlab("") +
scale_y_continuous(breaks = c(90, 180, 270, 360)) +
theme(panel.border=element_blank(),
panel.grid.minor=element_blank(),
panel.grid.major=element_blank(),
strip.text=element_blank(),
strip.background=element_blank(),
axis.text.y=element_blank(),
legend.position="none",
axis.ticks = element_blank()) +
scale_fill_manual(values = c("transparent", "transparent", "transparent", "transparent")) +
geom_vline(xintercept=4.8) +
geom_point(data=temp4, aes(x=Name, y=Score), color="black", shape=3, size=4) +
geom_point(data=temp3, aes(x=Name, y=Score), color="black") +
geom_point(data=temp5, aes(x=Name, y=Score), color="black", shape=3, size=2)

Resources