How to display weights/frequency of each word in wordcloud? - r

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")

Related

Change data label font in ggplot:

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.

How to change theme font for Volcano Plots?

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

How to change the breaks in the legend of my t_map object

I have created a map in tmap of all London Green space access points, with the amount of crimes counted in close proximity of the access point showing on the map via colour. However, with the final output as shown in this image.
map of green space access points and criminal activity
the breaks between each colour change in the classifier do not show the variation I am looking to achieve (breaks currently being (1, 501, 1001, 15001, 2000). I instead wanted to change these breaks to (1, 100, 500, 1000, 2000). Is there anyway I can do this as I have looked online but have found no answers. The code for the map output is bellow:
tm_shape(London_Ward_shp) +
tm_fill(palette = "grey") +
tm_borders(alpha=.3, col = "white") +
tm_shape(London_GS_Access_Points_Public) +
tm_bubbles(size = 0.05, col = "crime_2020", style = "pretty", scale = 1) +
tm_layout(main.title= "2020 Crime Levels near Public Park/Garden Access Points in London",
main.title.fontface = 1, fontfamily = "Helvetica",
legend.outside = TRUE, legend.position = c("right", "bottom"),
legend.title.size = 1, legend.title.fontface = 1, frame = FALSE)
a big thank you in advance!
You need to adjust the style parameter and add thebreaks parameter in your tm_bubbles() call:
tm_bubbles(size = 0.05, col = "crime_2020", style = "fixed", scale = 1, breaks = c(1, 100, 500, 1000, 2000))

contourplot color and labels options in Lattice for R

I am quite new to Lattice and I am stuck with some possibly basic coding. I am using shapefiles and geoTIFFS to produce maps of animals distribution and in particular I have:
1 x point shapefile
2 x geoTIFF
1 x polygon shapefile
I am overlapping a levelplot of one of the geoTIFF (UD generated with adehabitatHR) with a contourplot of the same geoTIFF at specific intervals (percentile values), a contourplot of the second geoTIFF (depth raster from ETOPO2) for three specific values (-200, -1000 and -2000), the point shapefile (animal locations) and the polygon shapefile (land). All works fine but I need to change the font size of contour plot labels, their length (i.e. from 0.12315 to 0.123) and positioning for all the contourplots. For the depth contourplot I would like to change the style of each line in something like "continous line", "dashed line" and "point line", and for the contourplot of the UD I would like to change the color of each line using a yellow to red palette.
As far as I understand, I should use panel functions to implement these changes (e.g. Controlling z labels in contourplot) but i am not quite sure how to do it. Part of my code to generate the "plot":
aa <-
quantile(
UD_raster,
probs = c(0.25, 0.75),
type = 8,
names = TRUE
)
my.at <- c(aa[1], aa[2])
depth<-c(-100, -200, -2000)
levelplot(
UD_raster,
xlab = "",
ylab = "",
margin = FALSE,
contour = FALSE,
col.regions = viridis(100),
main = "A",
maxpixels = 2e5
) + layer(sp.polygons(Land, fill = "grey40", col = NA)) + layer(sp.points(locations, pts = 2, col = "red")) + contourplot(
UD_raster,
at = my.at,
labels = TRUE,
margin = FALSE
) + contourplot(
ETOPO2,
at = depth,
labels = TRUE,
margin = FALSE
)
A simplified image, with no UD layer and no point shapefile can be found here and as you can see it is pretty messy. Thanks for your help.
So far for the ETOPO2 countourplot I have solved by eliminating the labels and adding the argument lty to style the line. Because I can't figure out how to use lty with different values for each single line in my contour, I have replicated the contourplot function three times on the same surface, one for each contour I am interested into (this was easy because I only need three contours).
For the position, font and font size of the labels of the remaining contourplot I have used
labels = list(cex = 0.8, "verdana"),
label.style = "flat"
To "shorten" the length of the labels I have used the function round where I specify to which decimal digit to round number.
So now my new code looks like:
aa <-
quantile(
UD_raster,
probs = c(0.25, 0.75),
type = 8,
names = TRUE
)
my.at <- c(aa[1], aa[2])
my.at <- round(my.at, 3)
levelplot(
UD_raster,
xlab = "",
ylab = "",
margin = FALSE,
contour = FALSE,
col.regions = viridis(100),
main = "A",
maxpixels = 2e5
) + layer(sp.polygons(Land, fill = "grey40", col = NA)) + layer(sp.points(positions, pts = 2, col = "red")) + contourplot(
UD_raster,
at = my.at,
labels = list(cex = 0.8, "verdana"),
label.style = "flat",
margin = FALSE
) + contourplot(
ETOPO2,
at = -200,
labels = FALSE,
margin = FALSE,
lty = 1,
pretty = TRUE
) + contourplot(
ETOPO2,
at = -1000,
labels = FALSE,
margin = FALSE,
lty = 2,
pretty = TRUE
) + contourplot(
ETOPO2,
at = -2000,
labels = FALSE,
margin = FALSE,
lty = 3,
pretty = TRUE
)
As one could expect, it takes a bit longer to produce the plot. Still no idea on how to change the colors of the UD contourplot.

How can I drop the comma from a tmap legend output?

I have this code:
tm_shape(usa, bbox = bbox, projection = map.crs)+
tm_borders(col = "grey", lwd = 1) +
tm_fill(palette = "grey") +
tm_grid(x = c(-77,-75,-73,-71),
y = c(39,37,35),
labels.inside.frame = F,
labels.size = 1.1,
col = "white") +
tm_layout(outer.margins = c(.1,.03,.05,.05),
outer.bg.color = "white")+
tm_shape(sk.dat1,axes = T) +
tm_dots("DATELAND", palette = colo,
auto.palette.mapping=FALSE,
size = .5,
breaks = seq(2010,2017,
length.out =8),legend.show = T, alpha=.7) +
tm_layout(title = "Southern Kingfish Occurrence",
legend.show = T,
legend.position = c("right","bottom"),
legend.bg.color = "grey70",
legend.height = 2)
Which outputs this legend. How can I change the tmap input to only include single years with no commas?
This looks as a number format problem; your code is not exactly reproducible, so I can not make 100% sure, but I am fairly certain by the look.
To remedy try including this in your tm_layout call:
legend.format=list(fun=function(x) formatC(x, digits=0, format="d"))
By the way I wrote a short blog post on tmap legend formatting a couple months back (my problem were currency and percentage signs).
https://www.jla-data.net/eng/tmap-legend/

Resources