Difficulty getting a legend to add to my graph - r

I have tried many ways to add a legend to the bottom two graphs but for some reason it either gives an error or doesn't show the legend.
Here is my code:
lg <- function(x, a = 1, b = 1){
exp(a+b*x) / (1+exp(a+b*x))
}
plot(NA, xlim=c(-5,5), ylim=c(0,1), xlab = "x", ylab = "y", legend(2, 0.4, legend=c("b = 1", "b = 2", "b = 3", "b = 4", "b = 5"))
for (b in c(1:5)){
curve(expr = lg(x, 1, b), from = -5, to = 5, n = 100, add= TRUE, col = b)
}
plot(NA, xlim=c(-5,5), ylim=c(0,1), xlab = "x", ylab = "y", legend(2, 0.4, legend=c("a = 1", "a = 2", "a = 3", "a = 4", "a = 5"))
for (a in c(1:5)){
curve(expr = lg(x, a, 1), from = -5, to = 5, n = 100, add= TRUE, col = a)
}
Is there something wrong with my placement of the legend within the code?

Put legend function in new row without comma before.
And close parenthesis of plot:
plot(NA, xlim=c(-5,5), ylim=c(0,1), xlab = "x", ylab = "y")
legend(2, 0.4, legend=c("b = 1", "b = 2", "b = 3", "b = 4", "b = 5"))
for (b in c(1:5)){
curve(expr = lg(x, 1, b), from = -5, to = 5, n = 100, add= TRUE, col = b)
}
same for plot2
plot(NA, xlim=c(-5,5), ylim=c(0,1), xlab = "x", ylab = "y")
legend(2, 0.4, legend=c("a = 1", "a = 2", "a = 3", "a = 4", "a = 5"))
for (a in c(1:5)){
curve(expr = lg(x, a, 1), from = -5, to = 5, n = 100, add= TRUE, col = a)
}

Related

Is there a way to change legend to show increasing and decreasing colors for waterfall plot using Plotly (r)?

I've plotted a waterfall chart/plot using plotly. I'm trying to change the legend so that it displays the increasing/decreasing colors (red/green) that I've set. Does anyone know how I would go about doing this? I'm try display only one legend for the entire figure rather than one legend for each subplot. Currently, what displays is the trace with a red and green box (as I've indicated in the picture).
Here is the data:
structure(list(Date = structure(c(1569888000, 1572566400, 1575158400,
1577836800, 1580515200, 1583020800, 1585699200, 1588291200, 1590969600,
1569888000, 1572566400, 1575158400, 1577836800, 1580515200, 1583020800,
1585699200, 1588291200, 1590969600, 1569888000, 1572566400, 1575158400,
1577836800, 1580515200, 1583020800, 1585699200, 1588291200, 1590969600
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), Percent_change = c(-45,
-50, -25, -30, -40, -35, -1, -5, -25, 30, 45, 50, -30, -40, -35,
-1, -5, -25, 50, -45, -30, -15, -20, -35, -1, -5, -25), Toys = c("Toy 1",
"Toy 1", "Toy 1", "Toy 1", "Toy 1", "Toy 1", "Toy 1", "Toy 1",
"Toy 1", "Toy 2", "Toy 2", "Toy 2", "Toy 2", "Toy 2", "Toy 2",
"Toy 2", "Toy 2", "Toy 2", "Toy 3", "Toy 3", "Toy 3", "Toy 3",
"Toy 3", "Toy 3", "Toy 3", "Toy 3", "Toy 3")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -27L))
Here is the code:
percent <- function(x, digits = 2, format = "f", ...) {
paste0(formatC(x, format = format, digits = digits, ...), "%")
}
my_plot <- . %>%
plot_ly(x = ~Date, y = ~Percent_change, type = "waterfall",
hoverinfo = "text",
hovertext = ~paste("Date :", Date,
"<br> % Change:", percent(Percent_change)),
increasing = list(marker = list(color = "red")),
decreasing = list(marker = list(color = "green")),
totals = list(marker = list(color = "blue")),
textposition = "outside", legendgroup = "trace 1") %>%
add_annotations(
text = ~unique(Toys),
x = 0.5,
y = 1,
yref = "paper",
xref = "paper",
xanchor = "middle",
yanchor = "top",
showarrow = FALSE,
font = list(size = 15),
yshift = 10
) %>%
layout(yaxis = list(title = "% Change",
ticksuffix = "%"),
xaxis = list(title = c("Date")),
showlegend =T)
example_data %>%
dplyr::filter(!is.na(Date)) %>%
group_by(Toys) %>%
distinct() %>%
do(p = my_plot(.)) %>%
subplot(nrows = 3, shareX = FALSE, titleY= TRUE, titleX= FALSE)
I would like the legend to specifically look like this with the title "Trend" above:
We can create two initial traces representing the two legend items.
After that we need to assign all increasing and decreasing traces into the legendgroups introduced with the initial traces and hide their legend items:
library(plotly)
library(dplyr)
library(data.table)
example_data <- structure(list( Date = structure(c(1569888000, 1572566400,
1575158400, 1577836800, 1580515200, 1583020800, 1585699200, 1588291200,
1590969600, 1569888000, 1572566400, 1575158400, 1577836800, 1580515200,
1583020800, 1585699200, 1588291200, 1590969600, 1569888000, 1572566400,
1575158400, 1577836800, 1580515200, 1583020800, 1585699200, 1588291200,
1590969600), class = c("POSIXct", "POSIXt"), tzone = "UTC"), Percent_change =
c(-45, -50, -25, -30, -40, -35, -1, -5, -25, 30, 45, 50, -30, -40, -35, -1,
-5, -25, 50, -45, -30, -15, -20, -35, -1, -5, -25), Toys = c("Toy 1", "Toy 1",
"Toy 1", "Toy 1", "Toy 1", "Toy 1", "Toy 1", "Toy 1", "Toy 1", "Toy 2", "Toy 2",
"Toy 2", "Toy 2", "Toy 2", "Toy 2", "Toy 2", "Toy 2", "Toy 2", "Toy 3",
"Toy 3", "Toy 3", "Toy 3", "Toy 3", "Toy 3", "Toy 3", "Toy 3", "Toy 3")),
class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -27L))
percent <- function(x, digits = 2, format = "f", ...) {
paste0(formatC(x, format = format, digits = digits, ...), "%")
}
my_plot <- . %>%
plot_ly(
x = ~ Date[1],
y = 0,
type = "bar",
name = "increasing",
color = I("darkgreen"),
legendgroup = "increasing",
showlegend = ~ all(showlegend)
) %>%
add_trace(
x = ~ Date[1],
y = 0,
type = "bar",
name = "decreasing",
color = I("red"),
legendgroup = "decreasing",
showlegend = ~ all(showlegend)
) %>%
add_trace(
x = ~ Date,
y = ~ Percent_change,
type = "waterfall",
# split = ~ legendgroup,
hoverinfo = "text",
hovertext = ~ paste("Date :", Date, "<br> % Change:", percent(Percent_change)),
increasing = list(marker = list(color = "red")),
decreasing = list(marker = list(color = "green")),
totals = list(marker = list(color = "blue")),
textposition = "outside",
legendgroup = ~ legendgroup,
showlegend = FALSE
) %>%
add_annotations(
text = ~ unique(Toys),
x = 0.5,
y = 1,
yref = "paper",
xref = "paper",
xanchor = "middle",
yanchor = "top",
showarrow = FALSE,
font = list(size = 15),
yshift = 10
) %>%
layout(yaxis = list(title = "% Change", ticksuffix = "%"),
xaxis = list(title = c("Date")),
legend = list(
itemclick = FALSE,
itemdoubleclick = FALSE,
groupclick = FALSE
))
example_data %>%
dplyr::filter(!is.na(Date)) %>%
mutate(legendgroup = case_when(
Percent_change >= 0 ~ "increasing",
Percent_change < 0 ~ "decreasing",
)) %>%
mutate(showlegend = data.table::rleid(Toys, legendgroup) %in% c(1, 2)) %>%
group_by(Toys) %>%
distinct() %>%
do(p = my_plot(.)) %>%
subplot(
nrows = 3,
shareX = FALSE,
titleY = TRUE,
titleX = FALSE
)
PS: if you prefer to display your waterfall using separate traces for the increasing and decreasing parts use split = ~ legendgroup in the add_trace call. Furthermore you'll need to set itemclick etc. back to TRUE in the layout call for an interactive legend.
You can edit the legend name in R and use javascript to edit the legend colors
Edit: I'll leave this here as it is a different approach which is sometimes useful, but I think the answer by #ismirsehregal - which doesn't involve hacking the object created by plotly.js - is better.
Steps:
Re-define your my_plot() function so that it names the first trace "decreasing" and the second one "increasing".
Append some javascript to manually change the legend colors.
Call the function, hiding the third legend, and appending the javascript
1. Redefine the function
This is the same as your function except it maps the first two groups to "increasing" or "decreasing".
my_plot <- function(x,
group_name,
groups_to_show_legend = c(
"Toy 1" = "decreasing", "Toy 2" = "increasing"
)) {
x %>%
plot_ly(
x = ~Date, y = ~Percent_change, type = "waterfall",
hoverinfo = "text",
hovertext = ~ paste(
"Date :", Date,
"<br> % Change:", percent(Percent_change)
),
increasing = list(marker = list(color = "red")),
decreasing = list(marker = list(color = "green")),
totals = list(marker = list(color = "blue")),
textposition = "outside",
legendgroup = "trace 1",
name = groups_to_show_legend[group_name]
) %>%
add_annotations(
text = ~ unique(Toys),
x = 0.5,
y = 1,
yref = "paper",
xref = "paper",
xanchor = "middle",
yanchor = "top",
showarrow = FALSE,
font = list(size = 15),
yshift = 10
) %>%
layout(
yaxis = list(
title = "% Change",
ticksuffix = "%"
),
xaxis = list(title = c("Date")),
showlegend = TRUE
)
}
2. Append some javascript
We can define some a javascript string in R which we feed to the htmlwidget created by plotly. This makes the "decreasing" symbol red and the "increasing" symbol green.
js_text <- htmltools::HTML('
let legend = document.querySelector(\'.scrollbox\');\n
let symbols = legend.getElementsByClassName("legendsymbols");\n
const re = new RegExp("fill: rgb.*?;", "ig");\n
symbols[0].innerHTML = symbols[0].innerHTML.replaceAll(re, "fill: rgb(255, 0, 0);");\n
symbols[1].innerHTML = symbols[1].innerHTML.replaceAll(re, "fill: rgb(0, 128, 0);");\n
')
3. Call the function, hiding the third legend, and appending the javascript
I've replaced do(), which is deprecated, with split() followed by purrr::imap(). This also allows us to supply the group names to the function:
example_data |>
dplyr::filter(!is.na(Date)) |>
group_by(Toys) |>
distinct() |>
split(~Toys) |>
purrr::imap(my_plot) |>
subplot(
nrows = 3,
shareX = FALSE,
titleY = TRUE,
titleX = FALSE
) |>
style(showlegend = FALSE, traces = 3) |>
htmlwidgets::prependContent(
htmlwidgets::onStaticRenderComplete(js_text)
)
We use htmlwidgets::prependContent() to attach this code to the plotly object, and htmlwidgets::onStaticRenderComplete() to ensure that it runs once the object is loaded.
You could use style to remove multiple traces. This creates one legend for your graph like this:
library(plotly)
library(dplyr)
my_plot <- . %>%
plot_ly(x = ~Date, y = ~Percent_change, type = "waterfall",
hoverinfo = "text",
hovertext = ~paste("Date :", Date,
"<br> % Change:", percent(Percent_change)),
increasing = list(marker = list(color = "red")),
decreasing = list(marker = list(color = "green")),
totals = list(marker = list(color = "blue")),
textposition = "outside", legendgroup = "trace 1") %>%
add_annotations(
text = ~unique(Toys),
x = 0.5,
y = 1,
yref = "paper",
xref = "paper",
xanchor = "middle",
yanchor = "top",
showarrow = FALSE,
font = list(size = 15),
yshift = 10
) %>%
layout(yaxis = list(title = "% Change",
ticksuffix = "%"),
xaxis = list(title = c("Date")),
showlegend = TRUE)
example_data %>%
dplyr::filter(!is.na(Date)) %>%
group_by(Toys) %>%
distinct() %>%
do(p = my_plot(.)) %>%
subplot(nrows = 3, shareX = FALSE, titleY= TRUE, titleX= FALSE) %>%
style(showlegend = FALSE, traces = c(1,2))
Created on 2023-02-08 with reprex v2.0.2

change second y axis color in base R

Change secondary line axis color changes send color for ggplot, but I chose to go with base R, and would like to be able to select the second y axis color.
I have the following data:
df = structure(list(A = c("Q4-17", "Q1-18", "Q2-18", "Q3-18", "Q4-18",
"Q1-19", "Q2-19", "Q3-19", "Q4-19", "Q1-20", "Q2-20", "Q3-20",
"Q4-20", "Q1-21", "Q2-21", "Q3-21", "Q4-21", "Q1-22", "Q2-22",
"Q3-22"), B = c(69.45, 71.1, 74.94, 73.87, 93.61, 91.83,
95.38, 109.8, 133.75, 125.26, 118.22, 145.65, 144.9757185, 155.3464032,
184.367033, 179.8121721, 187.235487, 189.1684376, 184.3864519,
161.5300056), C = c(70.73, 71.73, 74.33, 73.27,
95.94, 94.38, 95.38, 109.8, 115.32, 116.92, 115.9, 113.87, 106.108147,
96.84273563, 111.5150869, 110.1228567, 110.7448835, 194.9684376,
187.7241152, 167.7665553), D = c(260.3, 216.02, 203.72,
203.52, 300.96, 320.77, 330.5, 413.52, 436.7, 474.96, 463.6,
501.87, 493.8865461, 497.1760767, 514.9903459, 503.7601267, 510.8362938,
614.9915546, 603.5761107, 593.660831), E = c(NA,
NA, NA, NA, NA, NA, NA, NA, 39.237, 35.621, 32.964, NA, 152.137,
140.743023, 167.809, 170.877, 117.517, 102.691723, 88.8, 76.2445528
)), class = "data.frame", row.names = c(NA, -20L))
df = df %>%
rowwise() %>%
mutate(sums = sum(D,E, na.rm = TRUE))
df = df[8:nrow(df),]
and this to generate my plot
x <- seq(1,nrow(df),1)
y1 <- df$B
y2 <- df$D
par(mar = c(5, 4, 4, 4) + 0.3)
plot(x, y1, col = "#000000",
type = "l",
main = "title",
ylim = c(0, max(df[,2:3])),
ylab = "Y1",
xlab = "",
xaxt = "n")
axis(1,
at = seq(from = 13, by = -4, length.out = 4),
labels = df$A[seq(from = 13, by = -4, length.out = 4)])
lines(x, df$C, lty = "dashed", col = "#adadad", lwd = 2)
par(new = TRUE)
plot(x, df$sums, col = "#ffa500",
axes = FALSE, xlab = "", ylab = "", type = "l")
axis(side = 4, at = pretty(range(y2)),
ylim = c(0,max(df[,3:5], na.rm = TRUE)),
col = "#00aa00") # Add colour selection of 2nd axis
par(new = TRUE)
plot(x, df$D , col = "#0000ff",
axes = FALSE, xlab = "", ylab = "", type = "l", lwd = 1)
mtext("y2", side = 4, line = 3)
but this does not colour my complete second y axis, nor labels, nor title
does any one have any suggestions to be able to set entire y2 axis to be #00AA00 - ticks, labels, and title?

plot multiple datasets and compare categories with barplots

I have three datasets with the same variables and I want to compare one variables over 29 different categories between the three datasets. The example below should work as a reproducible example. I tried already to plot it but the out put was not as expected. I would like to have the three bars next to each other and a small plot in the plot for every category.
number_trackers = c(1, 2, 3, 4, 5, 6),
category = c("Ads", "Analytics", "Ads", "Analytics", "Ads", "Ads"),
c4 = c("url1.com","ur2.com","url3.com","url4.com","url5.com","url6.com"))
List_short_after=data.frame = c("Tracker1", "Tracker2", "Tracker3", "Tracker4","Tracker5","Tracker6"),
number_trackers = c(1, 2, 3, 4, 5, 6),
category = c("Ads", "Analytics", "Ads", "Analytics", "Ads", "Ads"),
c4 = c("url1.com","ur2.com","url3.com","url4.com","url5.com","url6.com"))
List_after=data.frame = c("Tracker1", "Tracker2", "Tracker3", "Tracker4","Tracker5","Tracker6"),
number_trackers = c(1, 2, 3, 4, 5, 6),
category = c("Ads", "Analytics", "Ads", "Analytics", "Ads", "Ads"),
c4 = c("url1.com","ur2.com","url3.com","url4.com","url5.com","url6.com"))
ggplot(data = NULL,
mapping = aes(y = number_trackers,x=category)) +
geom_col(data = List_before,fill= "#ca93ef", colour="#ca93ef") +
geom_col(data = List_short_after,fill= "#5034c4", colour="#5034c4") +
geom_col(data = List_after,fill= "#795fc6", colour="#795fc6") +
facet_wrap(facets = vars(category))+
theme_minimal() +
theme(text = element_text(color = "#795fc6",size=12,face="bold"),
axis.text = element_text(color = "#795fc6",size=14,face="bold"))+
labs( y = "Number Trackers", x = "Categories")
[![This is how the plot shut look like just with 3 bars instead of 2][1]][1]
[1]: https://i.stack.imgur.com/nDq36.png
Here's code that may help you reach your goal. Note that I took some liberties with your input data because it seems to be incomplete in your question.
library(ggplot2)
List_before <- data.frame(
list_id = "list_before",
name = c("Tracker1", "Tracker2", "Tracker3", "Tracker4","Tracker5","Tracker6"),
number_trackers = sample(c(1, 2, 3, 4, 5, 6)),
category = c("Ads", "Analytics", "Other 1", "Other 2", "Other 3", "Other 4"),
c4 = c("url1.com","ur2.com","url3.com","url4.com","url5.com","url6.com"))
List_short_after <- data.frame(
list_id = "list_short_after",
name = c("Tracker1", "Tracker2", "Tracker3", "Tracker4","Tracker5","Tracker6"),
number_trackers = sample(c(1, 2, 3, 4, 5, 6)),
category = c("Ads", "Analytics", "Other 1", "Other 2", "Other 3", "Other 4"),
c4 = c("url1.com","ur2.com","url3.com","url4.com","url5.com","url6.com"))
List_after <- data.frame(
list_id = "list_after",
name = c("Tracker1", "Tracker2", "Tracker3", "Tracker4","Tracker5","Tracker6"),
number_trackers = sample(c(1, 2, 3, 4, 5, 6)),
category = c("Ads", "Analytics", "Other 1", "Other 2", "Other 3", "Other 4"),
c4 = c("url1.com","ur2.com","url3.com","url4.com","url5.com","url6.com"))
df <- rbind(List_before, List_short_after, List_after)
df$list_id <- as.factor(df$list_id)
df$category <- as.factor(df$category)
ggplot(df, aes(y = number_trackers, x = list_id)) +
geom_bar(aes(fill = list_id), stat = "identity", position = position_dodge()) +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank()) +
facet_grid(~category) +
labs(y = "Number of Trackers", x = NULL)

R: non-numeric arguments to binary operators

I am working with the R programming language. I am trying to make a "parallel coordinates plot" using some fake data:
library(MASS)
a = rnorm(100, 10, 10)
b = rnorm(100, 10, 5)
c = rnorm(100, 5, 10)
d = matrix(a, b, c)
parcoord(d[, c(3, 1, 2)], col = 1 + (0:149) %/% 50)
However, a problem arises when I try to mix numeric and factor variables together:
group <- sample( LETTERS[1:4], 100, replace=TRUE, prob=c(0.25, 0.25, 0.25, 0.25) )
d = matrix(a,b, group)
parcoord(d[, c(3, 1, 2)], col = 1 + (0:149) %/% 50)
Error in x - min(x, na.rm = TRUE): non-numeric argument to binary operator
I am just curious. Can this problem be resolved? Or is it simply impossible to make such a plot using numeric and factor variables together?
I saw a previous stackoverflow post over here where a similar plot is made using numeric and factor variables: How to plot parallel coordinates with multiple categorical variables in R
However, I am using a computer with no USB port or internet access - I have a pre-installed version of R with limited libraries (I have plotly, ggplot2, dplyr, MASS ... I don't have ggally or tidyverse) and was looking for a way to do this only with the parcoord() function.
Does anyone have any ideas if this can be done?
Thanks
Thanks
One option is to label rows of the matrix using a factor and use that on the plot, e.g.
library(MASS)
set.seed(300)
par(xpd=TRUE)
par(mar=c(4, 4, 4, 6))
a = rnorm(12, 10, 10)
b = rnorm(12, 10, 5)
c = rnorm(12, 5, 10)
group <- sample(c("#FF9289", "#FF8AFF", "#00DB98", "#00CBFF"),
12, replace=TRUE)
d = cbind(a, b, c)
rownames(d) <- group
parcoord(d[, c(3, 1, 2)], col = group)
title(main = "Plot", xlab = "Variable", ylab = "Values")
axis(side = 2, at = seq(0, 1, 0.1),
tick = TRUE, las = 1)
legend(3.05, 1, legend = c("A", "B", "C", "D"), lty = 1,
col = c("#FF9289", "#FF8AFF", "#00DB98", "#00CBFF"))
EDIT
Thanks for the additional explanation. What you want does make sense, but unfortunately it doesn't look like it will work as I expected. I tried to make a plot using an ordered factor as the middle variable (per https://pasteboard.co/JKK4AUD.jpg) but got the same error ("non-numeric argument to binary operator").
One way I thought of doing it is to recode the factor as a number (e.g. "Var_1" -> 0.2, "Var_2" -> 0.4) as below:
library(MASS)
set.seed(123)
par(xpd=TRUE)
par(mar=c(4, 4, 4, 6))
a = rnorm(12, 10, 10)
b = c(rep("Var_1", 3),
rep("Var_2", 3),
rep("Var_3", 3),
rep("Var_4", 3))
c = rnorm(12, 5, 10)
group <- c(rep("#FF9289", 3),
rep("#FF8AFF", 3),
rep("#00DB98", 3),
rep("#00CBFF", 3))
d = data.frame("A" = a,
"Factor" = b,
"C" = c,
"Group" = group)
d$Factor <- sapply(d$Factor, switch,
"Var_1" = 0.8,
"Var_2" = 0.6,
"Var_3" = 0.4,
"Var_4" = 0.2)
parcoord(d[, c(1, 2, 3)], col = group)
title(main = "Plot", xlab = "Variable", ylab = "Values")
axis(side = 2, at = seq(0, 1, 0.1),
tick = TRUE, las = 1)
legend(3.05, 1, legend = c("A", "B", "C", "D"), lty = 1,
col = c("#FF9289", "#FF8AFF", "#00DB98", "#00CBFF"))
mtext(text = "Var 1", side = 1, adj = 0.6, padj = -30)
mtext(text = "Var 3", side = 1, adj = 0.6, padj = -12)
mtext(text = "Var 2", side = 1, adj = 0.6, padj = -21)
mtext(text = "Var 4", side = 1, adj = 0.6, padj = -3)

Label on X-axis for each group of points

I'm am creating a script which allows me to get data from a database, and visualises it in a graph.
As you can see, there are 8 groups of data, indicated on the X-axis. Each group always contains 90 values.
Right now, the place of the labels is hard-coded like this:
axis(1, at = c(31.25, 93.75, 156.25, 218.75, 281.25, 343.75, 406.25, 468.75),
labels = c("ss oligo 1", "ss oligo 2", "ss oligo 3", "ss oligo 4",
"ss oligo 4", "ss oligo 5", "ss oligo 6", "ss oligo 7"))
It works perfectly fine, but I was wondering if there is a way to do this more dynamically, by just telling R to assign a label to each set of 90 values.
Example:
# Generate data ###################################################################################
x <- vector()
y <- vector()
# y[length(y)+1] <- sample(10:12, 1, replace = TRUE)
oligo_1 <- runif(62, 10.5, 11.5)
oligo_2 <- runif(62, 14, 15)
oligo_3 <- runif(62, 17, 18)
oligo_4a <- runif(64, 20.5, 22)
oligo_4b <- runif(64, 20.5, 22)
oligo_5 <- runif(62, 24, 25)
oligo_6 <- runif(62, 27, 28)
oligo_7 <- runif(62, 30, 31)
y <- c(oligo_1, oligo_2, oligo_3, oligo_4a, oligo_4b, oligo_5, oligo_6, oligo_7)
x <- c(1:500)
example <- data.frame(x, y)
# Define variables ################################################################################
xmin <- 10
xmax <- 36
# Generate graph ###################################################################################
png(filename = "graph.png", width = 1500, height = 833)
plot(x = example[,2], type="l",
xlim = c(0, nrow(example)), ylim = c(xmin, xmax),
xaxt="n", yaxt="n",
xlab = "", ylab = "")
rect(xleft = par("usr")[1], ybottom = 9.8, xright = par("usr")[2], ytop = 12.2, border = "lightgrey", col = "lightgrey")
rect(xleft = par("usr")[1], ybottom = 13.2, xright = par("usr")[2], ytop = 15.5, border = "lightgrey", col = "lightgrey")
rect(xleft = par("usr")[1], ybottom = 16.5, xright = par("usr")[2], ytop = 18.9, border = "lightgrey", col = "lightgrey")
rect(xleft = par("usr")[1], ybottom = 19.9, xright = par("usr")[2], ytop = 22.3, border = "lightgrey", col = "lightgrey")
rect(xleft = par("usr")[1], ybottom = 23.3, xright = par("usr")[2], ytop = 25.5, border = "lightgrey", col = "lightgrey")
rect(xleft = par("usr")[1], ybottom = 26.5, xright = par("usr")[2], ytop = 28.7, border = "lightgrey", col = "lightgrey")
rect(xleft = par("usr")[1], ybottom = 29.7, xright = par("usr")[2], ytop = 32.1, border = "lightgrey", col = "lightgrey")
axis(1, at = c(31.25, 93.75, 156.25, 218.75, 281.25, 343.75, 406.25, 468.75),
labels = c("ss oligo 1", "ss oligo 2", "ss oligo 3", "ss oligo 4",
"ss oligo 4", "ss oligo 5", "ss oligo 6", "ss oligo 7"))
axis(2, at = c(11, 14.35, 17.7, 21.1, 24.4, 27.6, 30.9), las = 1)
lines(x = example[,2])
box()
mtext(paste("QC-check for", "TEST"), side = "3", line = 1, cex = 2, font = 2)
mtext("Samples" , side = "1", line = 3, cex = 1, font = 1)
legend(x = par("usr")[1]+10, y = par("usr")[4]-1, legend = c("Cq", "Ccq"), cex=1.5, lwd = 2, col = c("black","red"))
dev.off()
Why not:
axis(1, at=31.25+(0:7)*62.5, labels=paste("ss oligo",1:8) )
Alright, I made it much more difficult than it was. So in combo with the answer from #user449060, I changed the code to:
count <- nrow(data)/8
axis(1, at = count/2+(0:7)*count, labels = paste("ss oligo",c(1:4, 4, 5:7)))
which makes it a lot more dynamic!

Resources