I have several plots of the abundance of a particular species over my study area. I have been using image plot successfully. But now, I just want to plot presence/absence, so values from 0 to 1, and the plot stills shows me a continuous legend. Is there a way to modify this?
I have tried with image but the legend is not that cool.
This is my code:
colnames(zz)<-c("x","y","z")
ras<-rasterFromXYZ(zz)
asc<-asc.from.raster(ras)
image.plot(asc,
xlim=c(-9.5,-1),ylim=c(43,48),zlim=c(min(asc,na.rm=T),max(asc,na.rm=T)),
ylab="Latitude (ºN)",xlab="Longitude (ºW)",
col = viridis(2, option = "D"))
Related
I used the following lines to plot a heatmap with plotly in R:
plot_ly(data, x , y) %>% add_trace(type='histogram2dcontour')
I then obtained the following plot
The data actually looks like that on a scatter plot
As you can see, I lose a lot of points on the heatmap. I was wondering how I could manually set the scale for the colour of the heatmap for e.g. making it so that the colour changes every time the count increase by 10 instead of 100.
Otherwise, is there a better way to plot and visualize such data?
I have been able to plot several pie charts overtop a map, representing different populations. However, what I would like to do is somehow represent the sample size for each of the pie charts, as its differs between population. I have a loop to add each population present in the dataset as a pie chart:
map("worldHires", xlim=c(-140, -110), ylim=c(48, 64), col="lightgray", fill=TRUE)
points(x=-120.43,y=50.34, col="black", pch=19)
segments(x0=dataframe$Long, y0=dataframe$Lat, x1=dataframe$Long2, y1=dataframe$Lat2, col="black")
add.pie(z=c(2, 5, 6),x=-122.43,y=52.34,labels="",radius = 1)
for(i in 1:nrow(dataframe))
{
add.pie(as.integer(dataframe[i,c("Cat1","Cat2", "Cat3")]*100),
x=dataframe$Long2[i],y=dataframe$Lat2[i],labels="",radius = 0.08,
col=c("red","blue", "green"))
}
title(ylab="Latitude")
title(xlab="Longitude")
box(which="plot")
I would like to add the sample size data (dataframe$n) somehow. I've seen examples of scaled radius pie charts, which could work here, or even just adding the sample size above the pie chart. To get the sample size above the pie chart I tried adding 'main=dataframe$n' between labels and radius in the add.pie portion of the code, but this did not work. Does anyone have any ideas on how to add this to my script? Thank you.
The size of each pie is plotted according each value in your dataframe. The good dataframe for this has a stations as rows and the class type are columns
I want to make a graph that graphs box plots for two groups and adds a regression line for each group. I have seen a few examples available, but none achieving my goal.
My dataframe is like so:
df<- data.frame(cont.burnint= c(rep(2,10), rep(12, 10), rep(25, 10)),
variable= rep(c("divA","divC"), 30),
value= sample(x = seq(-1,4,0.5), size = 60, replace =
TRUE))
I would like to produce a graph like:
However, I want to change the points to a box plot for each group. I have not found helpful examples in the following:
Add geom_smooth to boxplot
Adding a simple lm trend line to a ggplot boxplot
The code I have found available thus far, changes my continuous variable cont.burnint to a factor and reorders the x-values from c(2,12,25) to c(12,2,25). Also, the regression lines in the ggplot examples (refer to link)do not extend to the y axis. I would like the regression line to extend to the y-axis. Thirdly, the box plots become off set from each other and I would like an option that keeps the box plot for both groups on the same x value.
So basically, I want to change the points in the graph provided to a box and whisker plot and keep all else the same, in the example above. I wouldn't mind adding a legend below the plot and making text and lines bolder too.
Here is the code for the example above:
plot(as.numeric(as.character(manovadata$cont.burnint)),manovadata$divA,type="p",col="black", xlab="Burn Interval (yr)", ylab="Interaction Diveristy", bty="n", cex.lab=1.5)
points(as.numeric(as.character(manovadata$cont.burnint)),manovadata$divC,col="grey")
abline(lm(manovadata$divA~as.numeric(as.character(manovadata$cont.burnint)), manovadata),col="black",lty=1)
abline(lm(manovadata$divC~as.numeric(as.character(manovadata$cont.burnint)), manovadata),col="grey",lty=1)
I can't imagine why you want overlaying boxplots, but here you go I think:
library(ggplot2)
df$cont.burnint <- as.factor(df$cont.burnint)
ggplot(df, aes(x=cont.burnint, y=value, col=variable))+
geom_boxplot(position=position_dodge(width=0), alpha=0.5)+
geom_smooth(aes(group=variable), method="lm")
I added some transparency to the boxplots using alpha to make them visible on top of each other.
Update:
ggplot(df, aes(x=cont.burnint, y=value, col=variable))+
geom_boxplot(aes(group=paste(variable,cont.burnint)))+
geom_smooth(aes(group=variable), method="lm", fullrange=T, se=F)+xlim(0,30)
Here's a fiddle for a simplified version of a plot I am trying to generate.
On line 44 the plot points are sized according to 1/Error:
main_aes = aes(x = Date, y = Popular_Support, size=1/Error)
But instead of displaying 1/Error values in the legend, I want it to display Sample Size which is 1/Error^2, which the legend title being Sample Size.
I only want this displayed in the legend, but I still want the original values to weight the point sizes.
How can I do this? How can I perform a calculation on the legend text that is displayed and change the legend title?
You can do this as follows:
plot + scale_size_continuous(breaks=seq(40,70,10), labels=seq(40,70,10)^2,
name="Sample Size")
Also, plot is an R function, so it's probably better to use a different name for your plot objects.
I have to draw a 20 plots and horizontally place a legends in each plots.
I gave the following command for the first plot:
plot(x=1:4,y=1:4)
legend("bottom",legend = c("a","b","c","d"),horiz=TRUE,text.font=2,cex=0.64)
then for the second plot I tried :
plot(x=1:2,y=1:2)
legend("bottom",legend = c("a","b"),horiz=TRUE,text.font=2,cex=0.64)
But because the size of the character vector passed to legend argument are different I get the size of the legend different.
Since I have to plot so many different plots having varying sizes of legends,I would want to do it in an automated fashion.
Is there a way to do this which can fix the size of the legend in all the plots and fit it to graph size?
par(cex=.64) at the beginning should suffice
op <- par(cex=.64) # this fix the legend size for all plots
plot(x=1:4,y=1:4)
legend("bottom",legend = c("a","b","c","d"),horiz=TRUE,text.font=2) # no need to set cex anymore
plot(x=1:2,y=1:2)
legend("bottom",legend = c("a","b"),horiz=TRUE,text.font=2)
par(op) # At end of plotting, reset to previous settings