I have the following data.table
:
m <- structure(list(std = c("0.3", "0.4", "0.5", "0.6", "0.7", "0.8",
"0.9", "1.0", "1.1", "1.2", "1.3", "1.4", "1.5", "1.6", "1.7",
"1.8", "1.9", "2.0"), r2 = c("0.8035", "0.8289", "0.8379", "0.8414",
"0.8447", "0.8462", "0.8476", "0.8486", "0.8493", "0.8479", "0.8420",
"0.8350", "0.8368", "0.8265", "0.8184", "0.8156", "0.8097", "0.7976"
)), row.names = c(NA, -18L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x00000195b9ea4e40>)
And this code where I create a line graph:
library(dplyr)
library(ggplot2)
library(data.table)
wd <- "path"
m <- fread(paste0(wd, "r2.csv"))
m$r2 <- format(m$r2, digits = 4)
m$std <- format(m$std, digits = 2)
# create the subset (if I want to highlight a specific point in the graph)
g1 <- subset(m, std == "1.1")
# plot the data
ggplot(m, aes(x = std, y = r2)) +
geom_line( color = "grey", linewidth = 0.7, group = 1) +
geom_point(shape = 21, fill = "darkgrey", size = 4, alpha = I(0.5)) +
geom_point(data = g1, shape = 21, fill = "black", size = 4) + # this adds a red point
geom_text(data = g1, label = "1.1", vjust = -0.8, hjust = 0) + # this adds a label for the red point
theme(
plot.title = element_text(color = "black", size = 17, face = "bold", hjust = 0.5),
plot.subtitle = element_text(size = 10, face = "bold", hjust = 0.5, color = "black"),
plot.caption = element_text(face = "italic", hjust = 0),
panel.background = element_rect(fill = 'transparent'),
axis.line.x = element_line(size = 1, linetype = "solid", colour = "lightgrey"),
axis.line.y = element_line(size = 1, linetype = "solid", colour = "lightgrey"),
) +
xlab("PSF width in units of pixels") + labs(y = expression ("R"^2)) +
theme(
axis.title.x = element_text(size = 20),
axis.title.y = element_text(size = 20),
axis.text = element_text(size = 17, color = "black")
)
This code gives me the following plot:
My question is, how can I plot every other number on the x and y axis? For example, I want on the x axis to show only the 0.3, 0.5, 0.7 and so on labels. On the y axis I want the 1st, 3rd, 5th and so on elements.
I have tried to use the scale_x_discrete
and other solutions I found online, but depending on the solution I'm getting errors.
R
4.3.2, RStudio
2023.12.1 Build 402, Windows 11.