R ggplot Order by Calculated value
Experimenting with an HR dataset, exploring why people 'left' the company (boolean value). Investigating the effect of job functions ('sales') while also showing salary for context. I would like to order the bars by the 'left' percentage and have had no luck doing so.
Any tips appreciated.
See also questions close to this topic

Fitting linear regression model in R
If my data set consists of only two variables,
x
andy
, wherex
is independent andy
is dependent, how would I go about making a fitted linear model about them? My attempt would be to first assign the model to variable result:summary(result<lm(y~x,data=DataTable))
How do I view the plot that describes this model? Once I find it, how do I tweak it to my needs? I'm trying to look up methods for this but I'm pretty lost when comparing it to my results. The data that I used can be found here https://ufile.io/2sjeh

Clear R memory using Rpy2
I have a bunch of R functions which I need to call through python. However, I reach memory errors when I try to allocate a large matrix. The same functions run fine on RStudio on the same computer. Here is a code chunk which crashes:
#python: import rpy2.robjects as ro import gc gc.collect() ro.r.source("calibration_functions.R") result1 = ro.r.func1() #Does some calculations, works fine. result2 = ro.r.func2(result1) #Crashes at this step #R code: func2 < function(result1){ preds_mat = matrix(data=NA, nrow = 263310, ncol = 1000) # do something... return(preds_mat) }
The error I get is: RRuntimeError: Error: cannot allocate vector of size 1004.4 Mb
How can I clean the R memory? gc() or gc.collect() doesn't work.

how do I change Correlation Matrix gradient color and convert anti diagonal correlation to diagonal?
I am able to generate a correlation matrix but then I want the gradient color to be changed. I would like to have lighter color for a lower value and darker color for higher value.
I would also appreciate if any can advice how do I flip the correlation diagonal from antidiagonal to diagonal, please refer the screenshot.
cormat < round(cor(test, use = "pairwise.complete.obs"),2) melted_cormat < melt(cormat) ggplot(data = melted_cormat, aes(x=Var1, y=Var2, fill=value)) + geom_tile(color='white')

How do I plot species as different colours in a point pattern (ppp) using spatstat in R?
The set up is this: There are 10 trees within a 20 by 20 m quadrat in a forest. For each tree we know the species, the diameter (in cm), and the location within the quadrat using x,y coordinates.
I would like to plot the trees within the quadrat, where the size of the points are to scale, and each species is represented by a different colour circle.
Use this data for an example:
tag < as.character(c(1,2,3,4,5,6,7,8,9,10)) species < c("A","A","A","A","B","B","B","C","C","D") diameter < c(50,20,55,30,30,45,15,20,35,45) x < c(9,4,5,14,8,19,9,12,10,2) y < c(6,7,15,16,12,4,19,2,14,9) df < data.frame(tag, species, diameter, x, y)
First I create the point pattern
species_map < ppp(df$x, df$y, c(0,20), c(0,20))
Then I mark the species and diameter
marks(species_map) < data.frame(m1 = df$species, m2=(df$diameter))
Now I can plot the point pattern and each point is to scale thanks to the marks on the diameter. The "markscale" bit is set to 0.01 because the diamter measurements are in cm and the quadrat size is defined in meters.
plot(species_map, which.marks=2, markscale=.01)
Now I want to make the circles of different species different colours, but this is where I'm stuck.
If I try to make a plot that includes both of my marks I just get 2 separate plots, with one using different size points to represent diameter (correctly) and one using different characters to represent different species.
plot(species_map, which.marks= c(1,2), markscale=.01)
How can I get this plot to represent different species using different colors of the same character while ALSO plotting the points to scale?
And how can I make it produce 1 single plot?
Thank you in advance.
Jay

apply ggplot color scale gradient to portion of data
I have a question about applying ggplot's color scale gradient. I have dataset where the response variable is a continuous variable including both positive and negative numbers, and the independent variable is a number of independent sites. I'm trying to plot the data in such a way that I can plot all of the data in the background and then apply a color scale gradient to response data that covers the negative range of the data. This is what I have so far with a example dataset that mimics the structure of my actual dataset.
tr_sim < data.frame(site_id = seq(1,100,1), estimated_impact = rnorm(100,18,200), impact_group = rep(c(1,2),each = 50)) rng_full < range(tr_sim$estimated_impact) #produce plot showing the full range of impacts across all sites and then over the subsetted sites impact_plot_full < ggplot(data = tr_sim, aes(x = factor(site_id, levels = site_id[order(estimated_impact)]), y = estimated_impact)) + geom_bar(stat = "identity",width = 1, fill = "grey80") impact_plot_full impact_plot_full + geom_bar(stat = "identity", width = 1, position = "stack", aes(y = estimated_impact[impact_group == 1])) + scale_fill_gradient2(low="firebrick", mid="yellow", high = "green4") + labs(y = "Estimated Impact ($/week)", x = "Total number of sites with estimate is 100", title = "Sites with the greatest impact after coverage loss") + theme(axis.text.x = element_blank()) + scale_y_continuous(breaks = round(seq(rng_full[1],rng_full[2],by=100),digits=0))
I can plot all of the data in the background in gray and I'm attempting to plot the negative range of the response data on top of this. I get the error that 'aesthetics must be either length 1 or the same as the data(100), y,x'. I know this is occurring because the negative data is not the same length as the entire dataset, but I can not figure out a way to do this. Any suggestions would be greatly appreciated.
Thank you, Curtis