Normally I provide some sample code for an reproducible example, but in this case it is a bit tricky. But I believe this question can be solved also without a reproduced example.
I have an image plot generated with the following code:
col <- c("red", "white", "blue") # set the color palette
image.plot(lon.list[[1]], lat.list[[1]], MK.list3[[6]][[8]],
main = paste0("Drought Index Trend \n", names(Index.list[[1]])[1]),
col = col,
xlab = "Longitude [°]",
ylab = "Latitude [°]",
legend.lab = "",
legend.line = 2.5,
zlim = c(-1,1))
plot(sf_object, add = T, border = "Black")
The resulting plot is the following:
I have a problem with the legend labels. Since there are only three values, -1, 0, 1, it is sufficient that only these values are displayed at the legend in the middle of each color.
So it should like this (the different colors don't matter):
Anybody with a hint how to achieve this?
Thanks to the comment of user20650, I found the solution. Here is the updated code which does what I want:
col <- c("red", "white", "blue") # set the color palette
image.plot(lon.list[[1]], lat.list[[1]], MK.list3[[6]][[8]],
main = names(Index.list[[1]])[8],
col = col,
xlab = "",
ylab = "",
legend.lab = "",
legend.line = 2.5,
zlim = c(-1,1),
axis.args = list(c(-1:1))) ### this is the new part ###
plot(sf_object, add = T, border = "Black")
Related
having issues getting three ellipses colors to show up. I am seeing all three shapes, but missing a color.
Also, I have two labels that I hope to delete. They could remain as points, but they aren't needed for the visual.
Here's my code-
#ordinate, nmds with bray-curtis distances
camord <- metaMDS(freq, distance = "bray", autotransform = FALSE)
#plotting ordination
ordiplot(camord, display='species')
ordilabel(camord, display="species",font=3, col = "black", cex = 0.5)
with(env, ordiellipse(camord, Habitat, col = as.numeric(envhab), lwd = 1.75))
legend("topright", legend = c(levels(envhab)), fill= as.numeric(factor(levels(envhab))))
I've been struggling with this for a day or two, tried a few things...
tried rearranging my CSV and using points...
points(camord, display = "species", label = colnames(camord)[1:11], cex = 0.5)
to just label the first 11 species.
Tried using text function... no labels again.
ordiplot(camord, display='species')
points(camord, display = "species", cex = 0.5)
with(env, ordiellipse(camord, Habitat, col = as.numeric(envhab), lwd = 2))
legend("topright", legend = c(levels(envhab)), fill= as.numeric(factor(levels(envhab))))
text(camord$species[1:11,1], camord$species[1:11,2], labels = colnames(camord)[1:11], cex = 0.5)
Many apologies as I am just beginning to learn. Any help or guidance would be appreciated.
i have the following plot:
p<- plot(c(0,1),c(0,1), type= "n", xlab = "", ylab = "", axes = FALSE)+
rect(0,0,.5,.5, col = "green")+
rect(.5,0,1,.5, col = "orange")+
rect(0,.5,.5,1, col = "yellow")+
rect(.5,.5,1,1, col = "red")
however instead of manually imputing the colors to the rectangles i would like to have a function [0,1] -> colors, where i have a value between 0 and 1 and it colors the boxes depending on the value. e.g:
(...)
rect(0,0,.5,.5, col = function(0.3))+
(...)
Heres one way to create that function
PercentageColour <- function(x){colorRampPalette(c('green','orange','yellow','red'))(101)[round(x*100)+1]}
p<- plot(c(0,1),c(0,1), type= "n", xlab = "", ylab = "", axes = FALSE)+
rect(0,0,.5,.5, col = PercentageColour(0.01))+
rect(.5,0,1,.5, col = PercentageColour(0.25))+
rect(0,.5,.5,1, col = PercentageColour(0.356))+
rect(.5,.5,1,1, col = PercentageColour(0.95))
Where colorRampPalette(colours) creates the ramp, (100) specifies how many steps you want in the ramp, and [round(x*100)] is rounding your percentage input and converting it to the indexed values for reference.
You can also use some of the default colour ramps if you don't want to make your own. Try rainbow or heat.colors for example
I have created a density plot with a vertical line reflecting the mean - I would like to include the calculated mean number in the graph but don't know how
(for example the mean 1.2 should appear in the graph).
beta_budget[,2] is the column which includes the different numbers of the price.
windows()
plot(density(beta_budget[,2]), xlim= c(-0.1,15), type ="l", xlab = "Beta Coefficients", main = "Preis", col = "black")
abline(v=mean(beta_budget[,2]), col="blue")
legend("topright", legend = c("Price", "Mean"), col = c("black", "blue"), lty=1, cex=0.8)
I tried it with the text command but it didn't work...
Thank you for your advise!
Something along these lines:
Data:
set.seed(123)
df <- data.frame(
v1 = rnorm(1000)
)
Draw histogram with density line:
hist(df$v1, freq = F, main = "")
lines(density(df$v1, kernel = "cosine", bw = 0.5))
abline(v = mean(df$v1), col = "blue", lty = 3, lwd = 2)
Include the mean as a text element:
text(mean(df$v1), # position of text on x-axis
max(density(df$v1)[[2]]), # position of text on y-axis
mean(df$v1), # text to be plotted
pos = 4, srt = 270, cex = 0.8, col = "blue") # some graphical parameters
I am creating a plot of some hydrology data and I am having trouble with the legend. The legend has a large amount of blank space around the text. I have tried using par(mar= list), cex, x.intersp, and anything else I could find with no luck. I can't post images yet, but there is a link below. This is all of the code I am using to create the plot and legend. The abline() below indicates the mean value of the data and that is what I am putting in the legend. I have researched on the site and I can make it work using mtext() or legend(no bg or box) and rect(), but it seems like I am missing something. I am fairly new to R, but I have written code in the past. I am using RStudio version 0.99.903 and R version 3.2.2.
plot(dataFile$time_s,dataFile$w_cmps, type = "l",col="grey50",
xlim = c(0,18.5), ylim = c(-7,7), xlab = "Time (s)", ylab = "w (cm/s)",
main = "Vertical Velocity", xaxt="n")
axis(side = 1, at = c(0,2,4,6,8,10,12,14,16,18),
labels = c(0,2,4,6,8,10,12,14,16,18))
abline(h = mean_w, col = "black", lty = 3, lwd = 4)
legend("bottomleft","Mean w", lty = 3, col = "red",
xjust = 1, cex = 1, bg = "grey75", trace = TRUE, ncol = 1, xpd = NA,
seg.len = 1, y.intersp = 0.25, x.intersp = 0.25)
I'm using base R plot(), and I want a legend (a color block and key) to show up above (outside) the top right of my plot next to my title (generated using title()).
What's the best way to do this?
Maybe something like this is what you're looking for:
x <- c(1,2,3,4)
y <- c(4,1,3,2)
z <- c(1,2,3,4)
dat <- data.frame(x,y,z)
windows(width = 5, height = 9) #quartz() on Mac
layout(matrix(c(1,2), 2, 1, byrow = TRUE), heights=c(0.5,1))
par(oma = c(4,3,0,0) + 0.1, mar = c(0,0,1,1) + 0.1)
plot(dat$x, y=rep(1,4), type = "n", axes = F, ylab = "", xlab = "")
legend(x = "bottomright", legend = c("y", "z"), fill = c("blue", "red"))
plot(dat$x, dat$y, type = "n", main = "PLOT")
lines(z, col = "red")
lines(y, col = "blue")
Basically this makes two plots, one is just invisible and shortened so all that's displayed is the legend.
You may be able to addtionally tweak the margins around the legend and other graphical parameters (?par) to get the layout better.