How to display a calculation on data values in R legend - r

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.

Related

Convert the continouos legend of an image.plot to discrete

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"))

How to prevent geom_text_repel from labeling points on scatter plot with default number ordering list?

My dataset looks like this:
I'm trying to create a simple scatter plot with data labels that are names (first and last name).
I used geom_text_repel in ggrepel to create data labels, but the labels on the plot are just numbers in the order of the data points in my dataset.
For example, if you look at the first datapoint, instead of the label being "Stephen Curry" it is "1"
I have no idea why this is happening and I can't find anyone else who even has my problem, let alone a solution.
Code:
ggplot(gravity,
aes(TS., USG., label = rownames(gravity))) +
geom_point(aes(TS., USG.), color='black') +
geom_text_repel(aes(TS., USG., label = rownames(gravity)))
The image above shows the plot created by the code. As you can see, the labels are just the ordering number instead of the name. I don't see why this happening considering those ordering numbers are not part of the dataset I imported.
Thanks in advance

Box plot of two groups add regression line to each group

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)

R: Is it possible to put a text box so many cm/inches above a line in graph? (Whithout changing scale of graph)

Is it possible to put a text box so many cm/inches above a line in graph in R? (Whithout changing scale of graph). So im plotting the image using plot and i want to specify that the text using the function: text() but I always want the text to be 1cm above the arrow at the specified x-coordinate.
enter image description here
You can do this simply with ?text. So not entirely with cm's but if you know the range of your data you can position your text as data points in the plot.
Example:
Let's make some test data.
dat <- matrix(rnorm(3*4), ncol=2)
colnames(dat) <- c("v1", "v2")
Create a scatter plot. You can of course apply text to any graphical plot, but I'm keeping it simple.
plot(v2 ~ v1, data = dat)
And now just create a line, in whatever direction. I'll just go with an abline at height 1.0 on the y-axis
abline(h=1.0)
text(1,1,"this is an abline", pos = 1)
With text I add text on position 1,1 (x, y) in the plot. I adjust it with pos so It doesn't get crossed by the abline.
Good luck!

Change the size of the text in legend according to the length of the legend vector in the graph

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

Resources