This solution https://stackoverflow.com/a/71590169/7106842
is effective a changing the font of the title, x-axis, and y-axis labels. But not of the annotated data labels. They are still default font. Is there a way to change those labels as well?
I recognize this isn't a minimally reproducible example but the code below was the original partial solution to the problem.
#incorrect model with labels added by sjPlot
a = (plot_model(sl_distr_model,
order.terms = c(1,2,3,4,5,6,7),
show.values = TRUE,
value.offset = .3,
rm.terms = c("taxon_detailAnas platyrhynchos","taxon_detailAnas strepera","logshape","logshape:cadencefactor1h","logshape:cadencefactor2h","logshape:cadencefactor3h","logshape:cadencefactor6h","logshape:cadencefactor12h","logshape:cadencefactor24h"),
axis.lim = c(xlimrange_min,xlimrange_max),
colors = c("firebrick"),
wrap.labels = 60,
title = c("Plot 3: Intercept parameters by time interval - Relationship bewtween shape and scale gamma parameters of step length distributions 7.429"),
axis.title = "Intercept = Red; Ratio of Shape to Rate parameters = Blue"))
#added 2 fonts I had installed
windowsFonts(A = windowsFont("Times New Roman"), B = windowsFont("Century Gothic"))
#incomplete formating
a + theme(text = element_text(family = "A")
The above code did not change the data label font. The all components of the chart were Times New Roman except for the data labels which remained the system deault (courier), to fix this I had to manually add the relevant values using geom_text() referencing the same font family as the rest of the chart
#correct model without data labels
aa = (plot_model(sl_distr_model,
order.terms = c(1,2,3,4,5,6,7),
show.values = FALSE,
value.offset = .3,
rm.terms = c("taxon_detailAnas platyrhynchos","taxon_detailAnas strepera","logshape","logshape:cadencefactor1h","logshape:cadencefactor2h","logshape:cadencefactor3h","logshape:cadencefactor6h","logshape:cadencefactor12h","logshape:cadencefactor24h"),
axis.lim = c(xlimrange_min,xlimrange_max),
colors = c("firebrick"),
wrap.labels = 60,
title = c("Plot 3: Intercept parameters by time interval - Relationship bewtween shape and scale gamma parameters of step length distributions 7.429"),
axis.title = "Intercept = Red; Ratio of Shape to Rate parameters = Blue"))
aa + theme(text = element_text(family = "B")) + geom_text(aes(label= round(sl_distr_model#beta[2:8],2)), family = "B", nudge_x = 0.25, check_overlap = F)
Now all text is Century Gothic.
Related
I'm trying to wrangle a Volcano plot made with the EnhancedVolcano package to have all text in Arial font style. I tried to do so with this code:
a <- EnhancedVolcano(data.matrix,
lab = rownames(data.matrix), x = "Log2.fold.change",
y = "P.value", xlim = c(-2, 2), ylim = c(0, 6), xlab = bquote(~ Log[2] ~ "fold change"),
ylab = bquote(~ -Log[10] ~ italic(P)), axisLabSize = 12,
title = paste("NanoString -", data.name), subtitle = "",
labFace = "bold", pointSize = 2, labSize = 5, pCutoff = 10e-2, FCcutoff = 0.4,
gridlines.major = FALSE, gridlines.minor = FALSE, drawConnectors = TRUE,
widthConnectors = 0.2, colConnectors = "black", legendPosition = "none"
)
print(a)
EV_merge <- a + theme(text = element_text(size = 8, family = "sans"))
print(EV_merge)
The problem I'm having is the element_text command, which I thought would work since the plot is an object in ggplot2, seems to only work for the axis and title text, but the font of the labels for the specific genes seems to remain the same. What function should I be using in this case?
The labels are nothing to do with theme. They are created by geom_text_repel, so they are part of a data layer. There doesn't seem to be an option to change the font family of the labels, but you can change it in situ after the plot is created.
Obviously, I don't have your data, but using the example from the help page and saving it as EV_merge, we have:
EV_merge
To change the font face and family, we can do:
EV_merge$layers[[4]]$aes_params$fontface <- 1
EV_merge$layers[[4]]$aes_params$family <- "sans"
EV_merge
Trying to change the line size of my confidence interval using ggsurvplot. When I use change the size to 1 or 2, it only affects the size of the center line, and not the size of the confidence interval lines. Any suggestions on how to alter the size of those lines?
Here is my code:
ggsurvplot(ovlsfit,
surv.scale="percent",
title="Survival Curve",
xlim=c(-0.02,5.1),
xlab=c("Time (y)"),
break.x.by=1,
censor=FALSE,
axes.offset=FALSE,
conf.int.style=c("step"),
legend=c("none"),
linetype = "strata",
color="black",
size=0.8,
risk.table=TRUE,
risk.table.height=0.18,
risk.table.pos=c("out"),
risk.table.fontsize=4)
And output:
Let's create a model similar to yours for demonstration:
library(survival)
library(survminer)
ovlsfit <- survfit(Surv(time/660, status) ~ 1, data = colon)
Now we will try to recreate your plot:
p <- ggsurvplot(ovlsfit,
surv.scale = "percent",
title = "Survival Curve",
xlim = c(-0.02,5.1),
xlab = c("Time (y)"),
break.x.by = 1,
censor = FALSE,
axes.offset = FALSE,
conf.int.style = c("step"),
legend = c("none"),
linetype = 'strata',
palette = "black",
size = 0.8,
risk.table = TRUE,
risk.table.height = 0.18,
risk.table.pos = c("out"),
risk.table.fontsize = 4)
p
Unfortunately, there is no direct access to the confidence interval line size from within the call to ggsurvplot, but we can easily access the layers they are in and change the line size directly. Here, we will make them large for demonstration purposes:
p$plot$layers[[3]]$aes_params$size <- 2
p$plot$layers[[4]]$aes_params$size <- 2
p
I am trying to customize a plot for competing risks using R and the package cmprsk. Specifically, I want to overwrite the default that for competing events colors are used and for different groups linetypes are used.
Here is my reproducible example:
library(ggplot2)
library(cmprsk)
library(survminer)
# some simulated data to get started
comp.risk.data <- data.frame("tfs.days" = rweibull(n = 100, shape = 1, scale = 1)*100,
"status.tfs" = c(sample(c(0,1,1,1,1,2), size=50, replace=T)),
"Typing" = sample(c("A","B","C","D"), size=50, replace=T))
# fitting a competing risks model
CR <- cuminc(ftime = comp.risk.data$tfs.days,
fstatus = comp.risk.data$status.tfs,
cencode = 0,
group = comp.risk.data$Typing)
# the default plot makes it impossible to identify the groups
ggcompetingrisks(fit = CR, multiple_panels = F, xlab = "Days", ylab = "Cumulative incidence of event",title = "Competing Risks Analysis")+
scale_color_manual(name="", values=c("blue","red"), labels=c("Tumor", "Death without tumor"))
Using ggplot_build() I managed to change the default regarding linetype and color, but I cannot find a way to add a legend.
p2 <- ggcompetingrisks(fit = CR, multiple_panels = FALSE, xlab = "Days", ylab = "Cumulative incidence of event",title = "Death by TCR", ylim = c(0, 1)) +
scale_color_manual(name="", values=c("blue","red"), labels=c("Tumor", "Death without tumor"))
q <- ggplot_build(p2)
q$data[[1]]$colour2 <- ifelse(q$data[[1]]$linetype=="solid","blue", ifelse(q$data[[1]]$linetype==22,"red", ifelse(q$data[[1]]$linetype==42,"green", ifelse(q$data[[1]]$linetype==44,"black", NA))))
q$data[[1]]$linetype <- ifelse(q$data[[1]]$colour=="blue","solid", ifelse(q$data[[1]]$colour=="red","dashed", NA))
q$data[[1]]$colour <- q$data[[1]]$colour2
q$plot <- q$plot + ggtitle("Competing Risks Analysis") + guides(col = guide_legend()) + theme(legend.position = "right")
p2 <- ggplot_gtable(q)
plot(p2)
Does anyone know how to add the legend to a plot manipulated by ggplot_build()? Or an alternative way to plot the competing risks such that color indicated group and linetype indicates event?
You don't need to go down the ggplot_build route. The function ggcompetingrisks returns a ggplot object, which itself contains the aesthetic mappings. You can overwrite these with aes:
p <- ggcompetingrisks(fit = CR,
multiple_panels = F,
xlab = "Days",
ylab = "Cumulative incidence of event",
title = "Competing Risks Analysis")
p$mapping <- aes(x = time, y = est, colour = group, linetype = event)
Now we have reversed the linetype and color aesthetic mappings, we just need to swap the legend labels and we're good to go:
p + labs(linetype = "event", colour = "group")
Note that you can also add color scales, themes, coordinate transforms to p like any other ggplot object.
I have created a wordcloud and, have used wordcloud() and ggplot(). I am unable to add the word frequencies to respective words in the cloud. I want the labels to appear in a single size and not in the size/weight of the associated word.
Can somebody help me add the labels? I have checked similar links:
How to display frequency in wordcloud
But I want the labels to be of standard size.
Sample code:
library(wordcloud)
wordcloud(BO$keywords, BO$bo, min.freq =1, scale=c(2.5,1), random.order = FALSE,
rot.per = 0,
colors = pal)+
geom_text_wordcloud_area(shape = "square", eccentricity = 2)+geom_text_repel(segment.size = 5, force = 1)
ggplot(BO, aes(label = keywords, size = bo), scale=c(6,5), min.freq = 1) +
geom_text_wordcloud(shape = "square", eccentricity = 2, area_corr=FALSE) +
scale_size_area(max_size = 6) +
theme_minimal()+
facet_wrap(~bo)+
scale_color_gradient(low = "lightblue", high = "darkblue")
I am using the R package GGPubr to make Boxplots. I really like the nice visuals that it provides but am having problems. Does anyone know how to increase the font size of the numbers on the axes, and the axis labels, and class labels? Also how do I set the mean values so that they only display 2 decimal places?
Here is the code that I'm using:
library("ggpubr")
mydata <- read.csv("C:\\temp\\ndvi.csv")
ggboxplot(mydata, x = "class", y = "NDVI",
color = "class",
order = c("Conifer", "Deciduous", "Grasslands"), ggtheme=theme_gray(),
ylab = "NDVI Value", xlab = "Land Cover Class",
add="mean",
font.label = list(size = 30, face = "bold"))+ stat_summary(fun.data
= function(x) data.frame(y=1, label = paste("Mean=",mean(x))), geom="text")
+theme(legend.position="none")
And the csv:
NDVI,class
0.25,Conifer
0.27,Conifer
0.29,Conifer
0.403,Deciduous
0.38,Deciduous
0.365,Deciduous
0.31983489,Grasslands
0.32005,Grasslands
0.328887766,Grasslands
I would prefer to achieve the desired effects above with GGPubr rather than boxplot() or ggplot/ggplot 2. Thanks.
Here is one option where we use round() to take care of the two decimal places and add another theme() to change the text size.
ggboxplot(mydata, x = "class", y = "NDVI",
color = "class",
order = c("Conifer", "Deciduous", "Grasslands"), ggtheme=theme_gray(),
ylab = "NDVI Value", xlab = "Land Cover Class",
add="mean",
font.label = list(size = 30, face = "bold")) +
# use round() and set y = .45
stat_summary(fun.data = function(x) data.frame(y=1, label = paste("Mean=", round(mean(x), 2))), geom="text") +
theme(legend.position="none") +
theme(text = element_text(size = 16)) # change text size of theme components