I am creating a multi panel figure that includes a ComplexHeatmap object so I am using multi_panel_figure() instead of ggarrange().
I have tried to include an argument to set the labels manually as I do in ggarrange but it does not work.
ggarrange(...,
font.label = list(size = 14, color = "black", face = "bold", family = NULL))
Like this:
Figure2 = multi_panel_figure(width = 360, height = 180,
columns = 3, rows = 1,
font.label = list(size = 24))
Figure2 %<>% fill_panel(DE_HM, column = 1)
Figure2 %<>% fill_panel(DE_TM, column = 2:3)
save_multi_panel_figure(figure = Figure2,
filename = "Figure2.svg")
I am quite new using ggplot2 so I do not know very well how to control the text size proportions. In this case I would like to make the labels bigger as I need to make the figure large so other text in it can be read easily. Maybe I should generate my figure any other way?
Here is how the labels look like right now (panel B need to be large so text can be read in the treemap):
With trace(fill_panel, edit=T) change the function fill_panel() on line 124 from:
panel_label <- textGrob(label = label, x = 1, y = 0, just = label_just)
to:
panel_label <- text_grob(label = label, x = 1, y = 0, just = label_just, face = "bold", size = 14)
also use library(ggpubr) in order to make this work.
Related
I want to create venn diagrams to emphasize that groups (circles) are completely located inside one another, i.e., there are no elements in the inner circles that are not simutanously in outer circles.
I've used ggvenn and arrived at these results:
colonias <- c("colônias")
possessoes <- c("possessões", colonias)
dominios <- c("domínios", possessoes, colonias)
ggvenn(tipologia_britanica,
show_elements = T,
label_sep = "\n",
fill_color = brewer.pal(name="Dark2", n=3),
fill_alpha = 0.6,
stroke_size = 0.2,
stroke_alpha = 0.2,
set_name_size = 5,
text_size = 5)
The result is tchnically correct because it show that "colonias" are common to all three groups and that "possessoes" are common to both "possessoes" and "dominios". But graphically I would like te groups to be completely inside one another to show that are no elements in "colonias" that are not common to all three, and in "possessoes" that are not common to "dominios". I'm not sure that ggvenn package is capable of plotting that.
One way may use the package eulerr.
However, your question isn't very clear so I let you play with the package
See the example below :
library(eulerr)
fit <- euler(c("A" = 10, "B" = 10, "A&B" = 8, "A&B&C"=3))
plot(fit,
fills = list(fill = c("red", "steelblue4","green"), alpha = 0.5),
labels = list(col = "black", font = 4),quantities = T)
I don't think ggvenn allows a plot with this kind of relationship. However, it's not terribly difficult to draw it yourself with ggplot and geom_circle from ggforce
ggplot(data.frame(group = c("domínios", "possessões", "colônias"),
r = c(3, 2, 1)),
aes(x0 = 3 - r, y0 = 0, fill = factor(group, group))) +
geom_circle(aes(r = r), alpha = 1) +
geom_text(aes(x = c(0, 1, 2), y = c(2.3, 1.3, 0), label = group),
size = 8) +
scale_fill_manual(values = c('#77bca2', '#e1926b', '#a09cc8'),
guide = 'none') +
coord_equal() +
theme_void()
I thought this should be easy but I've been having a hard time trying to modify the title of each plot. I'm just new to R so I really don't know how to edit this.
As you can see in the image, all the plot labels are centered but not properly placed. Some letters were cut out of the frame. These labels are automatically generated by ggdensity() when I combine 2 columns in one plot.
bell_shape <- lapply(slash_k, function(x) ggdensity(x, c("Mean_Lesion_Height", "Mean_Max_Lesion_Length"),
combine = TRUE,
add = "mean",
fill = "lightblue",
x.text.angle = 360,
color = "darkblue"))
pen_bell_shape <- ggarrange(plotlist = bell_shape,
widths = 10,
heights = 10,
nrow = 2,
ncol = 3,
labels = c("3.IR20", "5.IR20", "7.IR20", "3.TN1", "5.TN1", "7.TN1"),
font.label = list(size = 9, color = "red"))
I really have no idea how to edit this. I hope someone can help me.
I tried
bell_shape + theme(axis.title.x.top = c(size = 1, color ="block")
But it returns NULL
The solution to this problem is using
theme(strip.text())
This question has already been answered here:
[https://stackoverflow.com/questions/3290330/facet-label-font-size]
[https://stackoverflow.com/questions/2751065/how-can-i-manipulate-the-strip-text-of-facet-grid-plots]
[https://stackoverflow.com/questions/2631780/r-ggplot2-can-i-set-the-plot-title-to-wrap-around-and-shrink-the-text-to-fit-t]
This is also a good alternative
[https://stackoverflow.com/questions/2631780/r-ggplot2-can-i-set-the-plot-title-to-wrap-around-and-shrink-the-text-to-fit-t]
This can also provide insghts
[https://stackoverflow.com/questions/37089052/r-ggplot2-facet-grid-how-include-math-expressions-in-few-not-all-labels]
And here is how to change the facet labels
[https://stackoverflow.com/questions/3472980/how-to-change-facet-labels]
Documentation here
[https://rpkgs.datanovia.com/ggpubr/reference/facet.html]
As shown in the map below, how can I place the legend to the last cell in the grid?
The code I used is
psp1 <- tm_shape(province) +
tm_borders(col = 'black') +
tm_shape(county) +
tm_polygons(col = 'estimate', title = 'Changes in %', style = 'fixed', palette = brewer.pal(n = 6, name = 'Spectral'),
breaks = c(-15, -10, -5, 0, 5, 10, 15), legend.hist = F) +
tm_facets('warming', ncol = 2) +
tm_shape(province) +
tm_borders(col = 'black') +
tm_compass(north = 0, type = 'arrow', show.labels =0, position = c('right','top')) +
tm_layout(legend.format = list(fun = function(x) formatC(x, digits = 1, format = "f")),
fontface = 'bold',
legend.text.size = 1.3,
legend.width = 0.2,
legend.title.size = 1.5,
panel.label.size = 1.5,
panel.label.fontface = 'bold')
The data can be found from here. Thanks.
My answer comes perhaps a little late... (almost one year after your request!) Anyway, your question is very interesting and I hope this answer will be useful, either now or for future projects, for you or other SO users (by the way, thanks for having kept your input data accessible for a year ;-)).
As far as I know, it is not possible to solve your problem by using the tm_facets() function of the tmap library. So I suggest a slightly different "strategy" (still using the tmap library) to get what you are looking for.
It is articulated in two steps:
Build the maps and the legend manually... fortunately, not quite manually since the solution I suggest uses one custom function (i.e. make_graph()) that is run through a Map() function.
Edit the map mosaic with the legend using the R base grid library. Again, the implementation is made easier by the use of one custom function (i.e. Maps_setup()) run through a Map() function.
So, please find below a reprex that details the approach.
Reprex
STEP 1 - BUILDING THE MAPS AND THE LEGEND
library(sf)
library(tmap)
library(RColorBrewer)
# Import data
province <- st_read("province.shp")
county <- st_read("county.shp")
# Split the 'sf' object 'county' into a list of five 'sf' objects corresponding
# to the five warming scenarios (i.e. the first five facets of the final figure)
county_warm_list <- split(county , f = county$warming)
# Build the function 'make_graph' to generate the maps
make_graph <- function(x,y){
results <- tm_shape(x,
is.master = TRUE) +
tm_polygons(col = 'estimate',
title = 'Changes in %',
style = 'fixed',
palette = brewer.pal(n = 6, name = 'Spectral'),
breaks = c(-15, -10, -5, 0, 5, 10, 15),
legend.hist = FALSE,
midpoint = 0) +
tm_shape(province) +
tm_borders(col = 'black') +
tm_compass(north = 0,
type = 'arrow',
show.labels = 0,
position = c(0.93, 0.87),
size = 1.2) +
tm_layout(legend.show = FALSE,
# NB: the use of the 'get_asp_ratio()' function enables
# to optimize the size of each map inside its own facet:
asp = tmaptools::get_asp_ratio(x),
panel.labels = y,
panel.label.size = 0.8,
panel.label.fontface = 'bold',
inner.margins = c(0.02, 0.02, 0.02, 0.02))
return(results)
}
# Run the 'make_graph()' function through the list of the five 'sf' objects (i.e.
# 'county_warm_list') to generate the maps with their respective title using
# the 'Map()' function
map_titles <- names(county_warm_list)
Maps_list <- Map(make_graph, county_warm_list, map_titles)
# Build the legend using only the object "county"
Maps_legend <- tm_shape(county) +
tm_polygons(col = 'estimate',
title = 'Changes in %',
style = 'fixed',
palette = brewer.pal(n = 6, name = 'Spectral'),
breaks = c(-15, -10, -5, 0, 5, 10, 15),
legend.hist = FALSE,
midpoint = 0) +
tm_layout(legend.only = TRUE,
legend.position = c("center", "center"),
legend.format = list(fun = function(x) formatC(x, digits = 1, format = "f")),
fontface = 'bold',
legend.text.size = 1.3,
legend.width = 0.2,
legend.title.size = 1.5)
# Add the legend to 'Maps_list'
Maps_list$Legend <- Maps_legend
At the end of this first step, you get a list (i.e. Maps_list) containing 6 elements (i.e. five maps and one legend).
STEP 2 - LAYOUT OF THE MOSAIC OF MAPS WITH THE LEGEND
library(grid)
grid.newpage()
# Build the function 'Maps_setup' to set up the layout
Maps_setup <- function(x,y,z){
pushViewport(viewport(layout = grid.layout(nrow = 3, ncol = 2,
widths = unit(7.32, "cm"),
heights = unit(5, "cm"))))
setup <- print(x, vp = viewport(layout.pos.row = y, layout.pos.col = z))
return(setup)
}
# Run the 'Maps_setup()' function through the six objects of 'Maps_list' (i.e.
# 5 maps + 1 legend) to place the maps and the legend on the page using
# the 'Map()' function
# The 'pos_row' and 'pos_col' vectors are used to indicate where to place the
# maps as the 'Maps_setup()' function works through the list
pos_row <- rep(1:3, each = 2)
pos_col <- rep(1:2, times = 3)
Final_Results <- Map(Maps_setup, Maps_list, pos_row, pos_col)
Created on 2022-01-25 by the reprex package (v2.0.1)
I'm trying to convert my ggplot to a plotly plot using ggplotly(). However, it doesn't seem to work on this code, after manipulate is acted on the plot. Is there any other way to do it?
library(ggplot2)
library(manipulate)
grades <- data.frame(Final = 20 * runif(70))
myFinalsPlot <- function(sliderInput, initialIndex, finalIndex) {
ggplot(data.frame(grades$Final[initialIndex:finalIndex]),
aes(x = grades$Final[initialIndex:finalIndex])) +
geom_histogram(aes(y = ..density..),
binwidth = sliderInput, colour = "green", fill = "yellow") +
geom_density(alpha = 0.2, fill = "#FF6666") +
labs(x = "Marks", y = "Grades")
}
myFinalsPlot <- manipulate(myFinalsPlot(slidersInput, 1, 70),
slidersInput = slider(1, 12, step = 1, initial = 5))
First, to make your code work with the ggplot2 plot, there is an issue in your code that you need to fix. You shouldn't give the same name to your function and plot object. Replace this:
myFinalsPlot <- manipulate(myFinalsPlot(slidersInput, 1, 70),
slidersInput = slider(1, 12, step = 1, initial = 5))
By, e.g.:
myPlot <- manipulate(myFinalsPlot(slidersInput, 1, 70),
slidersInput = slider(1, 12, step = 1, initial = 5))
Now, regarding plotly plots, I don't think it is supposed to work with manipulate. I quote RStudio's website https://support.rstudio.com/hc/en-us/articles/200551906-Interactive-Plotting-with-Manipulate:
RStudio works with the manipulate package to add interactive capabilities to standard R plots.
I am wondering how to configure Jupyter to plot a smaller figure within R kernel.
I have tried using options(repr.plot.width = 1, repr.plot.height = 0.75, repr.plot.res = 300), but the result is kinda messy. It is changing the size of the plot R produced. Are there any ways I can directly configure the output graph size in Jupyter.
In other words, how can I change the size in the first figure to the size in the second figure, while not messing up the plot.
You need to manually set the tick size, marker size and text size. The text size and tick size can be set through the theme() function, while marker size through geom_point() function.
df_1 = data.frame(x=c(5, 6, 7, 8, 9), y = c(200, 225, 250, 270, 310))
options(repr.plot.width = 1, repr.plot.height = 0.75)
ggplot(df_1, aes(x = x, y = y)) + geom_point(size = 0.3) +
theme(text = element_text(size = 3), element_line(size = 0.1))
You should simply change the resolution of your plot. For instance, try repr.plot.res = 100 in:
options(repr.plot.width = 1, repr.plot.height = 0.75, repr.plot.res = 100)