我在创建结合斜体字母和变量输入的轴刻度标签时遇到问题。简单地说,我想调用变量并在每个标签下方插入诸如n = 1之类的文本。
这是一个除了斜体n之外的所有内容的示例:
require(ggplot2)
mpg$class <- as.factor(mpg$class)
counts <- rep(1:7, 1)
ggplot(mpg, aes(class, hwy)) +
geom_boxplot() +
scale_x_discrete(labels = paste(levels(mpg$class), "\nn = ", counts, sep = ""))
Run Code Online (Sandbox Code Playgroud)
我的第一个想法是使用 Unicode 表示斜体 N,就像我过去对上标数字所做的那样。但由于某种原因,这封信太小并且在风格上与文本的其余部分不匹配。更重要的是,在我的实际用例中,斜体 n 的所有 Unicode 字符似乎都呈现不对齐,在正常和下标之间的某个位置凹陷。
ggplot(mpg, aes(class, hwy)) +
geom_boxplot() +
scale_x_discrete(labels = paste(levels(mpg$class), "\n\U1D45B = ", counts, sep = ""))
Run Code Online (Sandbox Code Playgroud)
所以,我发现这个plotmath函数~italic(x)可以用来将字母变成斜体。paste()然而,当我将它合并到 x 轴刻度标签的行中时,出现了明显且意外的失败。请注意,该函数确实适用于xlab()
ggplot(mpg, aes(class, hwy)) +
geom_boxplot() + xlab(~italic("n")) +
scale_x_discrete(labels = paste(levels(mpg$class), "\n", ~italic("n"), " = ", …Run Code Online (Sandbox Code Playgroud) 当我做一个地图使用ggplot2,并尝试使用斜体的组合图标题的一部分expression(),并italic()使用一个字符串,我的地图出来的期望:
plottitle <- expression(paste('Example map with ', italic('italics')))
crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
states_map <- map_data("state")
map <- ggplot(crimes, aes(map_id = state)) +
geom_map(aes(fill = Murder),
map = states_map) +
expand_limits(x = states_map$long,
y = states_map$lat) +
labs(title = plottitle)
map
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试执行相同的操作,但使用对象而不是字符串时,该对象不会计算为所需的字符串:
word <- 'italics'
plottitle2 <- expression(paste('Example map with ', italic(word)))
map <- ggplot(crimes, aes(map_id = state)) +
geom_map(aes(fill = Murder),
map = states_map) +
expand_limits(x = states_map$long,
y = states_map$lat) + …Run Code Online (Sandbox Code Playgroud) 我想编辑情节的标题,以便它有四个单词,最后一个单词为粗体,例如:标题:“这是”(普通字体)“我的情节”(粗体)。
我已经尝试了一些在网上找到的代码,但是我只设法使该图的所有标题都加粗。我的代码(示例)看起来像这样,因为我也想更改标题的颜色和位置。现在,由于我的代码中的“ face = bold”,所有标题均以粗体显示。如上所述,我只希望最后两个单词以粗体显示,但在一行中,因此下面没有字幕或另一行。我正在使用ggplot2,将不胜感激!
plot4 <- plot4 + labs(title = "This is my plot")
plot4 <- plot4 + theme(plot.title=element_text(hjust=0.5, vjust=0.1, face='bold', colour="blue"))
Run Code Online (Sandbox Code Playgroud) 问题:是否可以在和 中指定的同一标签文本中同时使用plain和solid文本字体?scale_fill_manualscale_color_manual
我有
写于
ggplot(p, aes(x=value, y=os.neck, color=name_new, fill=name_new)) +
theme(axis.text.x = element_text(size=12, hjust=0)) +
geom_point(size=2.2, shape=21, stroke=1, fill=alpha("white", .7)) +
geom_quantile(quantiles=.5, size=1.3) +
scale_color_manual(values = c("#2C77BF", "#E38072", "#E1B930"),
name="",
labels=c("Lymph nodal yield\nUICC Stage I and II\nn=292", "Lymph nodal yield\nUICC Stage III and IV\nn=138","Lymph node density\nUICC Stage III and IV\nn=138")) +
scale_fill_manual(values = c("#2C77BF", "#E38072","#E1B930"),
name="",
labels=c("Lymph nodal yield\nUICC Stage I and II\nn=292", "Lymph nodal yield\nUICC Stage III and IV\nn=138","Lymph node density\nUICC Stage …Run Code Online (Sandbox Code Playgroud) 我正在尝试生成一个图表,并且我想将 .a 字符串的一部分加粗geom_text()。这是一些示例数据和图表:
temp <- data.frame(num = c("big"),
text = c("small bullet point of text"),
col = c("red"))
ggplot(data = temp) +
lims(x = c(0,100), y = c(0, 100)) +
geom_text(aes(x = 20, y = 50, label = num, color = col), size = 25) +
geom_text(aes(x = 60, y = 50, label = text), size = 3)
Run Code Online (Sandbox Code Playgroud)
理想情况下,我想做两件事。首先,最重要的是,我想给“子弹”这个词加气,而且只用那个词。其次,如果可能的话,我想将 Bullet 一词的颜色更改为 "red" ,以匹配 "big" 一词的颜色。
我在这里阅读了涉及bold()似乎来自grDevices包的功能的解决方案,但我无法让它工作。当我尝试该链接中的示例时,它会出现以下错误:
> paste("No. of ", bold("bacteria X"), …Run Code Online (Sandbox Code Playgroud)