- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
早上好。
我正在尝试使用 ggplot2 包进行绘图,但面临以下问题:
为了让它更容易理解,这是我想要制作的目标图像。
就像图像一样,我想做以下事情:
1)在虚线上方放置一个文本“中位数”,以便可以清楚地看到字符。
2)旋转三角形的度数(不是^ ^而是< >),使其有意义。
为了实现上述目标,到目前为止,我已经完成了以下代码:
# binding the data, defining the x and y aesthetics, title, labels
w_plot <- ggplot(
data = com_mal,
aes(x = reorder(name, -median_age), y = median_age)
)
labels = c('5 yrs old', 10, 15, 20, 25, 30)
w_plot +
geom_linerange(
aes(ymin = q1_age, ymax = q3_age),
color = "#76bde0",
size = 6,
alpha = 0.7
) +
geom_point(fill = "#ed3324", colour = "white", size = 4, shape = 21) +
geom_text(aes(y = 9, x = 15, label = '25th')) +
geom_text(aes(y = 20, x = 15, label = '75th percentile')) +
geom_text(aes(y = 30, x = 22, label = 'median')) +
geom_point(aes(y = 8.25, x = 15), shape = 17) +
geom_point(aes(y = 21.75, x = 15), shape = 17) +
geom_point(aes(y = 29, x = 21.9), fill = "#ed3324", colour = "white", size = 4, shape = 21) +
geom_hline(aes(yintercept = 5), linetype = 'dotted') +
geom_hline(aes(yintercept = 10), linetype = 'dotted') +
geom_hline(aes(yintercept = 15), linetype = 'dotted') +
geom_hline(aes(yintercept = 20), linetype = 'dotted') +
geom_hline(aes(yintercept = 25), linetype = 'dotted') +
geom_hline(aes(yintercept = 30), linetype = 'dotted') +
scale_y_continuous(breaks = seq(5, 30, by = 5), position = 'right', labels = labels) +
coord_flip() +
labs(title = 'Youngest Male Names',
subtitle = 'By estimated median age for Americans alive as of Jan 1. 2014',
x = NULL, y = NULL, caption = 'SOURCE: SOCIAL SECURITY ADMINISTRATION') +
theme(plot.title = element_text(face = 'bold', size = 16),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
axis.ticks = element_blank(), plot.caption = element_text(size = 10))
最佳答案
对于三角形,您可以使用 geom_text()
相反,将 family 参数设置为支持该字符的字体,对于标签使用 geom_label()
:
geom_text(label = "▶", size = 3, family = "HiraKakuPro-W3")
geom_label(aes(y = 4, x = 10, label = 'median'), fill = "grey92", label.size = NA)
label.size
删除标签的轮廓,“grey92”是(大约?)背景的颜色。
geom_label()
到线后的情节。 (另请注意,您可以在同一行代码中添加所有虚线。)
w_plot +
geom_linerange(
aes(ymin = q1_age, ymax = q3_age),
color = "#76bde0",
size = 6,
alpha = 0.7
) +
geom_point(fill = "#ed3324", colour = "white", size = 4, shape = 21) +
geom_text(aes(y = 9, x = 15, label = '25th')) +
geom_text(aes(y = 20, x = 15, label = '75th percentile')) +
geom_text(aes(y = 8.25, x = 15),label = "◀", size = 3,
family = "HiraKakuPro-W3") +
geom_text(aes(y = 21.75, x = 15),label = "▶", size = 3,
family = "HiraKakuPro-W3") +
geom_point(aes(y = 29, x = 21.9), fill = "#ed3324", colour = "white",
size = 4, shape = 21) +
geom_hline(yintercept = seq(5, 30, by = 5), linetype = 'dotted') +
geom_label(aes(y = 30, x = 22, label = 'median'),
fill = "grey92", label.size = NA) +
scale_y_continuous(breaks = seq(5, 30, by = 5),
position = 'right', labels = labels) +
coord_flip() +
labs(title = 'Youngest Male Names',
subtitle = 'By estimated median age for Americans alive as of Jan 1. 2014',
caption = 'SOURCE: SOCIAL SECURITY ADMINISTRATION') +
theme(plot.title = element_text(face = 'bold', size = 16),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
axis.ticks = element_blank(), plot.caption = element_text(size = 10))
关于r - 关于 ggplot2 : Rotate geom_point shape & Show geom_text above the line,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51854352/
我正在制作多个倾斜条形图的实例。由于计数的大小和百分比的差异不同,在某些情况下,其中一个标签(计数)的一部分被推到栏外。在所有情况下,我都需要标签完全位于栏内。如果不重新定位以适合栏内,我需要标签按原
所以我的散点图的一些标签重叠。我曾尝试将 direct.label 与方法“smart.grid”一起使用,但它不会产生适当的结果。这是我目前的图表: 生成它的代码: ggplot(d, aes(x=
我将 ggplot 与 facet_grid 一起使用,我想在每个方面指示每个方面的观察数量。我遵循许多网站上提供的示例,但是当我让它写任何东西时,它会在所有四个图上将所有四个观察数字都写在彼此之上。
我尝试添加文本,如 Position geom_text on dodged barplot 但它不适用于我的简单数据 data=data.frame(s=c(10,13,17,8),
我想调整条形图上的文本。 我尝试调整 hjust/vjust 以按我喜欢的方式显示,但它似乎无法正常工作。 ggplot(data) + geom_bar(aes(name, coun
我正在尝试复制以下代码( thomasp85/gganimate )。我有两个问题: 我不想显示第一个 geom_point 的标签(变量:partidos); 我希望动画结束后,最后一个 geom_
我正在尝试使用 ggplot 在 R 中绘制堆积条形图。我还想在该片段的每条条中包含百分比。我尝试关注帖子 1 , 2 , 3但这些值并不完全在各自的 block 中。我的数据是 dropbox 中的
我有一个使用 ggplot2 制作的条形图.我想使用 geom_text 为每个条添加标签使得标签的文本大小与标签相对应。为此,我使用了以下代码: a <- aggregate(mpg ~ vs +
在文本中插入数学符号不应该像我想象的那么复杂! OTH,甚至看起来相似的例子 ggplot2 facet_wrap with mathematical expression 我仍然无法将Ω(欧米茄)符
我希望仅在堆积条形图的顶部添加标签。 这是我的数据框: #create data frame building <- c("Burj \nKhalifa", "Zifeng \nTower", "Ba
这里我将 colorRed 设置为 TRUE 所以文本是红色的。但是当我将它设置为 FALSE 时,颜色仍然是红色。 如何根据 colorRed 的值设置文本颜色? library(ggplot2)
所以我正在使用 Twitter API 来收集与特定主题相关的信息,我正在可视化的事情之一是设备的流行程度。 到目前为止,我有这个: https://gyazo.com/441a9ab80b943f9
我正在绘制一个图,其中使用 geom_text 将 p 值绘制为文本。 pvalues 在文件 pvaluesmir21combined 中给出。 如果值 pvaluesmir21combined
我想使用 geom_text()(其中 ^ 表示上标)将“R^2 = 70%”添加到我的绘图中。 没有 % 符号,很简单: my.data <- data.frame(x=1:5, y=1:5) p1
希望这里有人能够帮助我解决我正在尝试解决的 ggplot 脚本问题。脚本会多次使用不同的数据,所以需要比较灵活。我几乎在我想要的地方得到了它,但我遇到了一个我无法解决的问题。 该脚本用于在右侧边距中为
我有一个用 GGplot2 制作的情节。现在,当我想更改图中文本点的大小时,文本的大小不会改变。我使用以下代码行: ggplot(data = out, aes(x = V2, y = V1)) +
我需要更改 geom_text() 中的数字格式包括逗号。 我已经看到了相关的问题,但我无法让这些解决方案发挥作用。我已经尝试过“sep =”一个,count/sum(count) 类型,以及我在不知
使用 mtcars ,我正在为 cyl 创建条形图并填写 gear .没有车 cyl==8 & gear==4 .绘制时,我使用了 position_dodge(preserve = "single"
我的问题 我想用一个表达式来注释 ggplot() 的每个方面,该表达式是统计测试产生的值的组合。我无法在 geom_text() 中解析字符串。 一个类似的问题被部分覆盖了here .不幸的是,我无
请考虑以下最小示例: library(ggplot2) library(ggrepel) ggplot(mtcars) + aes(x = mpg, y = qsec) + geom_line
我是一名优秀的程序员,十分优秀!