How to "bring to front" points plotted on in boxplot - ggplot2

As stated, I am trying to "bring to front" the points in the boxplot as shown in the below graph.
current plot
Here is the code I used to create the graph.
ggboxplot(dose2, x = "Group", y = "WTNeut", ylab = "NT50", palette = "jco", add = "point",
color = "black")+ scale_x_discrete(labels=c("Control","Infliximab","Ifx+Thio","Thiopurine","Tofacitinib","Ustekinumab","Vedolizumab"))+
theme(axis.text.x = element_text(angle=22.5, hjust=1))+
geom_boxplot(aes(fill = Group))+
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x)), limits = c(1e-1, 1e6))
I have tried to move the "add = "point"" to the front or to the back, however still didn't work.
Any thoughs would be greatfully appreciated! Thank you!

Related

Plotting sf file ontop of ggmap

I've been trying to plot a rivers system over a ggmap and hitting a bunch of walls. Hopefully there is a good solution.
Here is where I'm getting the rivers data:
https://data.review.fao.org/map/catalog/srv/api/records/6a53d768-1e20-46ea-92a8-c4040286057d
Loading in:
basemap <- get_stamenmap(bbox = c(left = 149.5, bottom = -35.9, right = 151.5, top = -32.5),
zoom = 3, maptype = 'terrain-background')
rr <- st_read("rivers_australia_37252/rivers_australia_37252.shp")
box = c(xmin = 145, ymin = -37, xmax = 155, ymax = -30)
rivers <- st_crop(rr, box)
class(rivers)
[1] "sf" "data.frame"
Plotting and problem code
ggmap(basemap) +
geom_sf(data=rivers, inherit.aes = FALSE)
# Error in st_cast.POINT(x[[1]], to, ...) : cannot create MULTILINESTRING from POINT
ggmap(basemap) +
geom_sf(data=rivers, aes(geometry), inherit.aes = FALSE)
# Error in is.finite(x) : default method not implemented for type 'list'
I then tried unlist() and it came up with a fortify error.
Any suggestions of how to transform the data or what to add in the geom_sf() code would be appreciated. Thanks!

mplcursors on multiaxis graph

In my program, im using mplcursors on a matplotlib graph so I can identify certain points precisely.
mplcursors.cursor(multiple=True).connect("add", lambda sel: sel.annotation.draggable(False))
Now I made a complex graph with multiple axis:
first = 1
offset = 60
for x in range(len(cat_list)):
if "Time" not in cat_list[x]:
if first and not cat_list[x].startswith("EngineSpeed"):
parasites[x] = ParasiteAxes(host, sharex = host)
host.parasites.append(parasites[x])
parasites[x].axis["right"].set_visible(True)
parasites[x].set_ylabel(cat_list[x])
parasites[x].axis["right"].major_ticklabels.set_visible(True)
parasites[x].axis["right"].label.set_visible(True)
p_plot, = parasites[x].plot(t, t_num_list[x], label = cat_list[x])
#parasites[x].axis["right"+str(x+1)].label.set_color(p_plot.get_color())
parasites[x].axis["right"].label.set_color(p_plot.get_color())
first = 0
elif not cat_list[x].startswith("EngineSpeed"):
parasites[x] = ParasiteAxes(host, sharex = host)
host.parasites.append(parasites[x])
parasites[x].set_ylabel(cat_list[x])
new_axisline = parasites[x].get_grid_helper().new_fixed_axis
parasites[x].axis["right"+str(x+1)] = new_axisline(loc = "right",
axes = parasites[x],
offset = (offset, 0))
p_plot, = parasites[x].plot(t, t_num_list[x])
parasites[x].axis["right"+str(x+1)].label.set_color(p_plot.get_color())
offset = offset + 60
host.legend()
fig.add_axes(host)
plt.show()
This code results in the following graph:
https://i.stack.imgur.com/Wl7yC.png
Now I have to somehow be able to select certain points by selecting which axis im using. How do I make a selection menu for choosing an active axis and how do I then use mplcursors to select my points?
Thanks,
Ziga

Center the plot title in ggsurvplot

I'm struggling with getting my plot title to the center using ggsurvplot...
I've seen some posts mentioning something like xxxx$plot + theme(....)
but this solution does not seem to work for me.
Here's my code, maybe you can see what I'm missing:
surv_object_CA19.9 <- Surv(time = data_OS$OS_Days / 30, event = data_OS$Status.Death)
CA19.9_surv_fit <- survfit(surv_object_CA19.9 ~ CA19.9.initial_status, data = data_OS)
CA19.9_OS <- ggsurvplot(CA19.9_surv_fit, data = data_OS, pval = TRUE, xlab = "Time [Months]",
ylab = "Overall survival", risk.table = TRUE, legend.title = "",
risk.table.col. = "strata", risk.table.y.text = FALSE, surv.scale = "percent",
break.x.by = 6, xlim = c(0, 60), legend.labs = c("Pathological", "Normal"),
title = "Overall survival for patients with initially pathological or normal CA19-9 values",
CA19.9_OS$plot + theme(plot.title = element_text(hjust = 0.5)))
Thank you for any help! I'm still new to R and not particularly a friend of it yet, so any tips are highly appreciated!
One relatively easy solution is to define your own custom theme based off of the theme that is used in ggsurvplot(). Looking at the documentation for the function shows us that it is applying via ggtheme= the theme theme_survminer(). We can create a custom function that uses %+replace% to overwrite one of the theme elements of interest from theme_survminer():
custom_theme <- function() {
theme_survminer() %+replace%
theme(
plot.title=element_text(hjust=0.5)
)
}
Then, you can use that theme by association with the ggtheme= argument of ggsurvplot():
library(ggplot2)
library(survminer)
require("survival")
fit<- survfit(Surv(time, status) ~ sex, data = lung)
ggsurvplot(fit, data = lung, title='Awesome Plot Title', ggtheme=custom_theme())
#Add parameters to your theme as follows
centr = theme_grey() + theme(plot.title = element_text(hjust = 0.5, face = "bold"))
#Fit the model
fit<- survfit(Surv(time, status) ~ sex, data = lung)
#create survival plot
ggsurvplot(fit, data = lung, title="Your Title Here", ggtheme=centr)

Changing color scale in a geom_tile

I'm trying to change the color scale in a geom_tile from a single color to a gradient.
I don't have any error message in my code;
I had a look at other messages, the cookbook etc there is a lot for a beginner (too much)...
Here is the initial code, which gives a "red gradient" map:
`ggplot(pacific.maps,aes(x = long, y = lat),na.rm)+
geom_polygon(aes(group = group,fill = region),bg = "grey")+
scale_x_continuous(name="Longitude E", limits=c(100, 180)) +
scale_y_continuous(name="Latitude S", limits=c(-50, -1))+
theme(panel.background = element_rect(fill = "azure1",colour = "azure1"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())+
geom_tile(data=Y2010,aes(x=LON_F2,y=LAT_F2),alpha=0.1,fill = "red",na.rm=TRUE)`
If i try to change these lines below, I get grey tiles :(
`geom_tile(data=Y2010,aes(x=LON_F2,y=LAT_F2),alpha=0.1) +
scale_fill_manual(values=(brewer.pal(3, "RdYlBu")),na.value="white")`
I haven't put any data as I think it's most probably a syntax problem (and also because I used the reprex in R studio and that's all what I got!)
I hope this won't take more than a few minutes for experienced people.
Thank you for your help.

Legend not showing. Error in strwidth(legend, units = "user", cex = cex, font = text.font) : plot.new has not been called yet

I have the code below that is a combination of two boxplots and dot plots in one. It is a representation of barring density in 4 different species. The grey depicts the males and the tan the females.
data<-read.csv("C:/Users/Jeremy/Documents/A_Trogon rufus/Black-and-White/BARDATA_boxplots_M.csv")
datF<-read.csv("C:/Users/Jeremy/Documents/A_Trogon rufus/FEMALES_BW&Morphom.csv")
cleandataM<-subset(data, data$Age=="Adult" & data$White!="NA", select=(OTU:Density))
cleandatF<-subset(datF, datF$Age=="Adult", select=(OTU:Density))
dataM<- as.data.frame(cleandataM)
dataF<- as.data.frame(cleandatF)
library(ggplot2)
ggplot(dataM, aes(factor(OTU), Density))+
geom_boxplot(data=dataF,aes(factor(OTU),Density), fill="AntiqueWhite")+
geom_boxplot(fill="lightgrey", alpha=0.5)+
geom_point(data=dataF,position = position_jitter(width = 0.1), colour="tan")+
geom_point(data=dataM, position = position_jitter(width = 0.1), color="DimGrey")+ scale_x_discrete(name="",limits=order)+
scale_y_continuous(name="Bar Density (bars/cm)")+
theme(panel.background = element_blank(),panel.grid.minor=element_blank(),
panel.grid.major=element_blank(),axis.line = element_line(colour = "black"),
axis.title.y = element_text(colour="black", size=14),
axis.text.y = element_text(colour="black", size=12),
axis.text.x = element_text(colour="black", size=14))
This works just fine.
However, when I try to add a legend as:
legend("topright", inset=.01, bty="n", cex=.75, title="Sex",
c("Male", "Female"), fill=c("lightgrey", "black")
It returns the following Error:
Error in strwidth(legend, units = "user", cex = cex, font = text.font) :
plot.new has not been called yet
Please, is there someone who could suggest how to correct this?