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
Strange behavior of ggplot2 stacked bar plot, why groups are not grouped together properly?
I have a data frame like the following:
set.seed(1) mydf <- data.frame(ID="id1", Type=c(rep("C",6),rep("A",4)), Value=abs(rnorm(n=10))) mydf$Value.log <- log2(mydf$Value+0.1) mydf # ID Type Value Value.log # 1 id1 C 0.6264538 -0.46105702 # 2 id1 C 0.1836433 -1.81785019 # 3 id1 C 0.8356286 -0.09599211 # 4 id1 C 1.5952808 0.76152426 # 5 id1 C 0.3295078 -1.21924386 # 6 id1 C 0.8204684 -0.11955993 # 7 id1 A 0.4874291 -0.76751348 # 8 id1 A 0.7383247 -0.25441895 # 9 id1 A 0.5757814 -0.56537156 # 10 id1 A 0.3053884 -1.30262333
I make this stacked bar plot and it looks good:
png(filename="test.png") print(#or ggsave() ggplot(mydf, aes(ID, Value.log, fill=Type)) + geom_bar(stat="identity") ) dev.off()
Why if I just change the name of
Type"A" to "E", keeping the rest exactly the same, the stacked bar plot does not do the grouping the same way??
mydf$Type <- as.character(mydf$Type) mydf$Type[which(mydf$Type=="A")] <- "E" mydf$Type <- factor(mydf$Type) png(filename="test2.png") print(#or ggsave() ggplot(mydf, aes(ID, Value.log, fill=Type)) + geom_bar(stat="identity") ) dev.off()
Type"C" (now in red) broken in two now? How can I avoid it? Thanks!
Plotting 2 unevenly spaced time series on a single graph using R
I have 2 data sets of ocean temperatures for the past 2 million years.
The first data set is from the atlantic and has 70 observations unevenly spaced over a 2 million year interval. The second data set is from the pacific and has 500 observations also unevenly spaced over the same interval.
I want to plot both data sets on one graph so I can compare the temperature differences between the 2 oceans over the last 2 million years.
Any and all suggestions welcome!!
Changing data structure in R
I am currently having the following data structure (as data.frame)
NA a1 a2 a3
t1 y11 y21 y31
t2 y12 y22 y33
t3 y13 y23 y33
and want to change it into
t1 s1 y11
t2 s1 y12
t3 s1 y13
t1 s2 y21
t2 s2 y22
t3 s2 y23
t1 s3 y31
t2 s3 y32
t3 s3 y33
any suggestion how to proceed? Thanks for your help ;)
R pie charts distorted when adding to projected map using ggplot
I want to plot pie charts onto a projected map using ggplot. However, the pie charts become distorted, probably due to the projection. Does anyone know how I can plot the pie charts without the distortion? Example code is below, thanks.
lib = c("ggplot2","scatterpie") lapply(lib, library, character.only=TRUE) pie = data.frame( lon=c(-5.0,-3.5,-5.5,5.0), lat=c(50.0,50.2,50.1,50.5), A=c(0.25,0.75,0,0.25), B=c(0.75,0.10,0,0.75), C=c(0,0.15,1,0), radius=0.05) world = map_data("world", resolution=0) ggplot(data=world, aes(x=long, y=lat, group=group)) + geom_polygon(data=world, aes(x=long, y=lat, group=group), fill="darkseagreen", color="black") + coord_map(projection = "mercator",xlim=c(-7.0,-2.0), ylim=c(49,52)) + geom_scatterpie(aes(x=lon, y=lat, r=0.15), data=pie, cols=c("A","B","C"), color="black", alpha=0.9) + ylab("Latitude\n") + xlab("Longitude") + theme( panel.background = element_rect(fill="lightsteelblue2"), panel.grid.minor = element_line(colour="grey90", size=0.5), panel.grid.major = element_line(colour="grey90", size=0.5), legend.position = "top")