How to add automated data labels to line plot? - r

I've produced a simple line plot using base R and want to add data labels to the point. Any idea how to do this in an automated way? Picture of graph produced here
plot(grant$year, grant$grantee, type = "o", xlab = "Year", ylab = "Number of Grantees", pch = 16, col = "dark blue", lwd = 3, cex = 2)

I estimated your data from the linked picture. By adding text(grant$year, grant$grantee, labels = grant$grantee, pos = 3) after your plot gives us labels. pos = 3 puts the labels above the data points.
year <- c(2015,2016,2017,2018,2019,2020)
grantee <- c(50,55,51,30,52,83)
grant <- data.frame(year, grantee)
plot(grant$year, grant$grantee, type = "o", xlab = "Year", ylab = "Number of Grantees", pch = 16, col = "dark blue", lwd = 3, cex = 2)
text(grant$year, grant$grantee, labels = grant$grantee, pos = 3)

Related

How can I change the colour of my points on my db-RDA triplot in R?

QUESTION: I am building a triplot for the results of my distance-based RDA in R, library(vegan). I can get a triplot to build, but can't figure out how to make the colours of my sites different based on their location. Code below.
#running the db-RDA
spe.rda.signif=capscale(species~canopy+gmpatch+site+year+Condition(pair), data=env, dist="bray")
#extract % explained by first 2 axes
perc <- round(100*(summary(spe.rda.signif)$cont$importance[2, 1:2]), 2)
#extract scores (coordinates in RDA space)
sc_si <- scores(spe.rda.signif, display="sites", choices=c(1,2), scaling=1)
sc_sp <- scores(spe.rda.signif, display="species", choices=c(1,2), scaling=1)
sc_bp <- scores(spe.rda.signif, display="bp", choices=c(1, 2), scaling=1)
#These are my location or site names that I want to use to define the colours of my points
site_names <-env$site
site_names
#set up blank plot with scaling, axes, and labels
plot(spe.rda.signif,
scaling = 1,
type = "none",
frame = FALSE,
xlim = c(-1,1),
ylim = c(-1,1),
main = "Triplot db-RDA - scaling 1",
xlab = paste0("db-RDA1 (", perc[1], "%)"),
ylab = paste0("db-RDA2 (", perc[2], "%)")
)
#add points for site scores - these are the ones that I want to be two different colours based on the labels in the original data, i.e., env$site or site_names defined above. I have copied the current state of the graph
points(sc_si,
pch = 21, # set shape (here, circle with a fill colour)
col = "black", # outline colour
bg = "steelblue", # fill colour
cex = 1.2) # size
Current graph
I am able to add species names and arrows for environmental predictors, but am just stuck on how to change the colour of the site points to reflect their location (I have two locations defined in my original data). I can get them labelled with text, but that is messy.
Any help appreciated!
I have tried separating shape or colour of point by site_name, but no luck.
If you only have a few groups (in your case, two), you could make the group a factor (within the plot call). In R, factors are represented as an integer "behind the scenes" - you can represent up to 8 colors in base R using a simple integer:
set.seed(123)
df <- data.frame(xvals = runif(100),
yvals = runif(100),
group = sample(c("A", "B"), 100, replace = TRUE))
plot(df[1:2], pch = 21, bg = as.factor(df$group),
bty = "n", xlim = c(-1, 2), ylim = c(-1, 2))
legend("topright", unique(df$group), pch = 21,
pt.bg = unique(as.factor(df$group)), bty = "n")
If you have more than 8 groups, or if you would like to define your own colors, you can simply create a vector of colors the length of your groups and still use the same factor method, though with a few slight tweaks:
# data with 10 groups
set.seed(123)
df <- data.frame(xvals = runif(100),
yvals = runif(100),
group = sample(LETTERS[1:10], 100, replace = TRUE))
# 10 group colors
ccols <- c("red", "orange", "blue", "steelblue", "maroon",
"purple", "green", "lightgreen", "salmon", "yellow")
plot(df[1:2], pch = 21, bg = ccols[as.factor(df$group)],
bty = "n", xlim = c(-1, 2), ylim = c(-1, 2))
legend("topright", unique(df$group), pch = 21,
pt.bg = ccols[unique(as.factor(df$group))], bty = "n")
For pch just a slight tweak to wrap it in as.numeric:
pchh <- c(21, 22)
ccols <- c("slateblue", "maroon")
plot(df[1:2], pch = pchh[as.numeric(as.factor(df$group))], bg = ccols[as.factor(df$group)],
bty = "n", xlim = c(-1, 2), ylim = c(-1, 2))
legend("topright", unique(df$group),
pch = pchh[unique(as.numeric(as.factor(df$group)))],
pt.bg = ccols[unique(as.factor(df$group))], bty = "n")

How can I make the y axis on a r plot display numbers in decimal format with commas?

I am running the following R code:
plot(datereal, casesimm8lock7,
type = "l", lwd = 1, col = "red",
main = "a Sensitivity to time delays: 8 month immunity",
xaxt = "n", xlab = "Month",
ylab = "Daily new cases", ylim = c(0,250000))
And I want the y-axis, which currently displays 50000 100000 150000 etc. to display 50,000 100,000 150,000 etc.
I am trying to use the scales package but haven't figured it out yet.
Start by making an empty plot with no y axis. Then plot the data.
Then plot the y axis with axis(2, ...). In order to have the labels formatted like the question asks for, use help("formatC").
# test data
datereal <- Sys.Date() - 10:0
casesimm8lock7 <- seq(0,250000, length.out = 11)
# the plot
plot(datereal, casesimm8lock7, type = "n", yaxt = "n")
lines(datereal, casesimm8lock7,
lwd = 1, col = "red",
main = "a Sensitivity to time delays: 8 month immunity",
xaxt = "n", xlab = "Month",
ylab = "Daily new cases", ylim = c(0,250000))
axis(2, at = seq(0, 250000, by = 50e3),
labels = formatC(seq(0, 250000, by = 50e3),
format = "d", big.mark = ","))
With package scales, it could be with either label_comma, like below, or label_number. Thse functions return a labeller function, to be applied to the vector of axis marks.
labeller <- scales::label_comma()
plot(datereal, casesimm8lock7, type = "n", yaxt = "n")
lines(datereal, casesimm8lock7,
lwd = 1, col = "red",
main = "a Sensitivity to time delays: 8 month immunity",
xaxt = "n", xlab = "Month",
ylab = "Daily new cases", ylim = c(0,250000))
axis(2, at = seq(0, 250000, by = 50e3),
labels = labeller(seq(0, 250000, by = 50e3)))

How to start a line in plot from the end of the existing line?

I would like to draw three dotted lines all of them at the end of the existing line. This is my dataset and code:
x = data.frame(Debt = c(115.413 , 116.522 , 123.361, 129.021, 131.786, 131.557, 131.397, 131.355, 132.1, 134.77))
future = data.frame(144.9, 147.9, 150.9)
plot(x$Debt, lwd = 2, lty = 1, type = "l", ylab = "", xlab ="", col = "red", xaxt = "n")
lines(????)
axis(1, at = seq(2010, 2020, 1), labels = seq(2010, 2020, 1))
abline(v = 2019, col = "black", ldy = 3)
legend("bottomright", col = c("black", "blue", "green"), bty = "n", lty = 1)
An example of the plot I would like to get is:
In my case the lines that I want to draw from x$Debt are the points in future.
Can anyone help me?
This solution is not beautiful at all, but you can try this.
From your initial dataframes, x and future, create new dataframe combined two data, and slice the dataframe to show
The code is as follow:
x <- data.frame(Debt = c(115.413 , 116.522 , 123.361, 129.021, 131.786, 131.557, 131.397, 131.355, 132.1, 134.77))
future <- data.frame(Debt=c(144.9, 147.9, 150.9))
df <- data.frame(x=c(1:(nrow(x)+nrow(future))), y= c(x$Debt, future$Debt))
plot(range(df[,1]), range(100,150), type='n')
lines(df[1:nrow(x),1], df[1:nrow(x),2], type='l', col='black')
lines(df[nrow(x):nrow(df),1], df[nrow(x):nrow(df),2], type='l', col='blue')
If you want to multiple lines in future range, you can append the data with years and values to the df, and add lines() with proper slices.

R legend not showing in Plot

I have the following R code for a simple plot:
ExperimentDataNames = c('Count', 'HumanData', 'ActualPrices')
ExperimentData <- read_csv("/Users/justin_chudley/desktop/ExperimentData.csv", col_names = ExperimentDataNames)
x <- Count <- ExperimentData$Count
y <- HumanData <- ExperimentData$HumanData
y1 <- ActualPrices <- ExperimentData$ActualPrices
plot(x,y, type = "l", xlab="Trial Number",ylab="$USD",main="Dow Jones Price vs Human Experiment")
lines(x,y1, type = "l", col=2)
legend=c('Human Data', 'Actual Prices')
The legend does not show at all in this plot for some reason:
Why is my legend not showing?
With your coding, you have assigned a vector of characters to an object named legend.
In order to add a legend, you need to use the legend() function.
legend(x = 10, y = 4e5,
col = c("black", "red"), lty = 1, lwd = 1,
legend = c('Human Data', 'Actual Prices'))
You can use a heuristic approach by varying the values in x and y until you find a position you like. Alternatively, you can also set x to one of several predefined values:
legend(x = "top",
col = c("black", "red"), lty = 1, lwd = 1,
legend = c('Human Data', 'Actual Prices'))
Other options are to set x to "bottomright", "bottom", "bottomleft", "left", "topleft", "topright", "right" or "center".

Plot problems in R

I create a data frame with four different rows. Every row has decreasing numbers. But I have some problems with the plot in the picture:
Everything is correct with the plot except that the grey bars are the wrong way round. Actually they would have to rise.
This is the syntax:
plot(z_RMSE_MAE$max_diff, z_RMSE_MAE$z_images_left, col.lab="black",yaxt='n',type = "o", xlab = "max Differenz", ylab ="", main ="",col = "red")
axis(4, col = "red")
par(new = T)
barplot(z_RMSE_MAE$z_MAE, type= "h", col = "grey",xlab ="", ylab = "",space = 0.8, density = 40, decreasing ="FALSE")
mtext(side = 2, line = 3, col = "grey" ,'MAE')
mtext(side = 4, line = 3, col = "red" ,'images left')
axis(side = 2 )
Can someone help me?

Resources