Plot problems in R - 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?

Related

How to add automated data labels to line plot?

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)

Adjust distance between x-axis and text in R plot

In the plot below, you probably find the distance between x-aixs and the date (Jan-01-60 to Mar-31-16) is large.
Is there anyway I could make the distance smaller?
Here is my example code:
plot(dates, data, type = "l", lwd = 3, ylab = " ",
xlab = " ", col = "gray35", xaxt='n', ann = FALSE)
axis(side=1, at=dates_ten, labels=format(dates_ten, "%b-%d-%y"), las =
1, cex.axis=0.5, las = 1, font = 2, tcl = -0.2)
Many thanks.
You can use the padj argument to axis
# make a reproducible example
dates <- seq(as.Date("2016/1/1"), as.Date("2016/4/1"), "days")
dates_ten <- seq(as.Date("2016/1/1"), as.Date("2016/4/1"), "10 days")
set.seed(42)
data <- rnorm(seq_along(dates))
Vary padj as needed:
plot(dates, data, type = "l", lwd = 3, ylab = " ",
xlab = " ", col = "gray35", xaxt='n', ann = FALSE)
axis(side=1, at=dates_ten, labels=format(dates_ten, "%b-%d-%y"), las = 1,
cex.axis=0.5, las = 1, font = 2, tcl = -0.2, padj = -2)

R need to make this process faster--for loop, function, or something else

I have a set of code, below, and I need to run it 120 times, with the variables modified, of course, for each run. I tried a for loop and I tried to create a function. I just want to do this process faster. Can someone please help me? The variables I need to have modified are score and seconds. So, it would be score1, seconds1 for the first run, score2, seconds2 for the second run, and so on. Thank you.
pre <- aggregate(score~seconds, dataframe, FUN = sum)
freqsdf <- data.frame(table(SecsOnly$seconds))
condp <- pre$score/freqsdf$Freq
condp <- data.frame(condp)
par(mar = c(5,5,5,5))
hist(dataframe$seconds, xlab = "", ylab = "", ylim = c(0, 25), axes = FALSE, col = "grey", main = 'title here', breaks = 100)
axis(4, ylim = c(0, 25), col = "black", col.axis = "black", las = 2, cex.axis = 1)
par(new = TRUE)
plot(pres$seconds, condp$condp, col = "red" , type = "l", xlab = "Time", ylab = "y axis label", ylim=c(0,1.0))
abline(h = 0.20)

Put one line chart and bar chart in one plot in R (Not ggplot)?

how to
Combine a bar chart and line in single plot in R (from different data sources)?
Say I have two data sources as:
barData<-c(0.1,0.2,0.3,0.4) #In percentage
lineData<-c(100,22,534,52,900)
Note that they may not be in the same scale.
Can I plot both barData and LineData in one plot and make them good looking ?
I cant use ggplot in this case so this is not a duplicated question..
Something like the following:
Maybe this helps as a starting point:
par(mar = rep(4, 4))
barData<-c(0.1,0.2,0.3,0.4) * 100
y <- lineData<-c(100,22,534,900);
x <- barplot(barData,
axes = FALSE,
col = "blue",
xlab = "",
ylab = "",
ylim = c(0, 100) )[, 1]
axis(1, at = x, labels = c("Julia", "Pat", "Max", "Norman"))
ats <- c(seq(0, 100, 15), 100); axis(4, at = ats, labels = paste0(ats, "%"), las = 2)
axis(3, at = x, labels = NA)
par(new = TRUE)
plot(x = x, y = y, type = "b", col = "red", axes = FALSE, xlab = "", ylab = "")
axis(2, at = c(pretty(lineData), max(lineData)), las = 2)
mtext(text="Lines of code by Programmer", side = 3, line = 1)
box()

Scale secondary y-axis in combination graph of barplot and line

I made a graph in which a barplot and a line plot are combined. The problem is that the scale of my secondary y-axis isn't how it should be.
This is the code I used:
barplot <- barplot(covpatient[[1]]$cov, names.arg = covpatient[[1]]$exon, xlab = covpatient[[1]]$gene[1] , ylab = "read depth" , border = gray.colors(length(unique(covpatient[[1]]$exon)))[as.factor(covpatient[[1]]$exon)])
par(new = TRUE)
lines(x = barplot, y = covpatient[[1]]$amplicon, bty = "n")
axis(side = 4, at = pretty(range(covpatient[[1]]$amplicon)))
And this is how my plot looks like:
The values of the lines plot are OK, but you see that the y-axis is not fully expanded. I want it to look the same as the y-axis on the left
Can someone help me with this?
Without a reproducible example and a clear question all answers will at best be gueswork but have a look at the following:
x <- 1:10
y <- c(1, 3, 5, 6, 2, 7, 11, 3, 2, 13)
z <- runif(10, min=1000, max=10000)
par(mar = c(5, 4, 4, 4) + 0.3)
barplot(y, col=rainbow(length(x)))
par(new = TRUE)
plot(x, z, type = "l", axes = FALSE, bty = "n", xlab = "", ylab = "")
axis(side=4, at = pretty(range(z)))
mtext("z", side=4, line=3)
library(plotrix)
twoord.plot(x,y,x,z,
xlab="x",
ylab="y",
rylab="z",
main="Main",
type=c("bar","l"),lcol=rainbow(length(x)),rcol=4)

Resources