Changing color scale in a geom_tile - ggplot2

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.

Related

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

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!

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)

Percentage labels in pie chart with ggplot

I'm working now in a statistics project and recently started with R. I have some problems with the visualization. I found a lot of different tutorials about how to add percentage labels in pie charts, but after one hour of trying I still don't get it. Maybe something is different with my data frame so that this doesn't work?
It's a data frame with collected survey answers, so I'm not allowed to publish them here. The column in question (geschäftliche_lage) is a factor with three levels ("Gut", "Befriedigend", "Schlecht"). I want to add percentage labels for each level.
I used the following code in order to create the pie chart:
dataset %>%
ggplot(aes(x= "", fill = geschäftliche_lage)) +
geom_bar(stat= "count", width = 1, color = "white") +
coord_polar("y", start = 0, direction = -1) +
scale_fill_manual(values = c("#00BA38", "#619CFF", "#F8766D")) +
theme_void()
This code gives me the desired pie chart, but without percentage labels. As soon as a I try to add percentage labels, everything is messed up. Do you know a clean code for adding percentage labels?
If you need more information or data, just let me know!
Greetings
Using mtcars as example data. Maybe this what your are looking for:
library(ggplot2)
ggplot(mtcars, aes(x = "", fill = factor(cyl))) +
geom_bar(stat= "count", width = 1, color = "white") +
geom_text(aes(label = scales::percent(..count.. / sum(..count..))), stat = "count", position = position_stack(vjust = .5)) +
coord_polar("y", start = 0, direction = -1) +
scale_fill_manual(values = c("#00BA38", "#619CFF", "#F8766D")) +
theme_void()
Created on 2020-05-25 by the reprex package (v0.3.0)

Plotly Facets not translating properly

I've found that with Plotly with R, when I'm faceting plots, they often don't translate properly from R to Plotly.
For example, my graph plotted in R looks like so:
When I send it to plotly, it looks like so:
(Some data has been hidden from both plots for confidentiality reasons)
My code looks like so:
plot <- ggplot(sytoxG_data_no_NC) +
geom_ribbon(data = confidence_intervals_SG, mapping = aes(x = time_elapsed, ymin = phenotype_value.NC.lower, ymax = phenotype_value.NC.upper,
fill = "red", colour = NULL), alpha = 0.6) +
scale_fill_manual(name = "Legend",
values = c('red'),
labels = c('Negative Control')) +
xlab("Time Elapsed") +
ylab("Sytox Green") +
ggtitle("Sytox Green - Facets: Pathway") +
facet_wrap(~Pathway, ncol=6, scales = "fixed") +
theme(panel.grid = element_blank(),
axis.ticks.length = unit(0, "cm"),
panel.background = element_rect(fill = "white"),
strip.text.x = element_text(size=4),
axis.text = element_blank())
response <- py$ggplotly(plot, kwargs=list(world_readable=FALSE, filename="SG_sparklines_by_pathway", fileopt="overwrite"))
The issue might very well be with geom_ribbon rather than facets... Can you please upgrade your "plotly" package and give it another try?
I wound up using facet_grid instead of facet_wrap. Something like this:
+ facet_grid(~Pathway, scales = "free", space="free")

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?