- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试复制我在一篇论文中发现的精彩情节。该图基本上有一个中心 y 轴,然后根据变量在左侧和右侧创建条形图。接下来是剧情:
我有一个类似的数据框来复制这个图。我的数据框 df
具有下一个结构:
df
# A tibble: 64 x 4
Gender Cases Deaths Group
<chr> <int> <dbl> <chr>
1 F 6163 24 G1
2 M 9067 136 G1
3 F 430 4 G2
4 M 1026 51 G2
5 F 43 0 G3
6 M 67 1 G3
7 F 1382 43 G4
8 M 888 26 G4
9 F 249 4 G5
10 M 191 10 G5
# ... with 54 more rows
我想创建上面提到的图,在中心轴上显示变量 Group
在 x 轴(左和右)中,我想显示变量 Cases
和 Deaths
根据 Gender
变量,这将是 M
左边的条形图F
的性别和权利障碍性别。
为了达到目标情节,我草拟了一些可能作为基础的代码,但我不知道如何修改它以更改轴的顺序。这是代码:
library(ggplot2)
library(tidyverse)
#Code for F gender
df %>% pivot_longer(-c(Group,Gender)) %>%
filter(Gender=='F') %>%
mutate(Group=factor(Group,levels = unique(df$Group),ordered = T)) %>%
ggplot(aes(x=Group,y=value,fill=name))+
geom_bar(stat = 'identity')+
scale_x_discrete(limits = rev(unique(df$Group)))+
coord_flip()+
theme(legend.position = 'top')
#Code for F gender
df %>% pivot_longer(-c(Group,Gender)) %>%
filter(Gender=='M') %>%
mutate(Group=factor(Group,levels = unique(df$Group),ordered = T)) %>%
ggplot(aes(x=Group,y=value,fill=name))+
geom_bar(stat = 'identity')+
scale_x_discrete(limits = rev(unique(df$Group)))+
coord_flip()+
theme(legend.position = 'top')
产生下一个图:
我在这些图中发现的一个问题是,尽管按照我的意愿显示了条形图,但它们有两个问题。首先,我希望其中一个条形图(例如死亡)可以比另一个条形图(例如案例)更细。其次,柱的结构正在累积所有值。我希望这两个度量在每个轴上都从零开始。
我希望这个情节可以在ggplot2
中创作.我的数据df
接下来是:
#Data
df <- structure(list(Gender = c("F", "M", "F", "M", "F", "M", "F",
"M", "F", "M", "F", "M", "F", "M", "F", "M", "F", "M", "F", "M",
"F", "M", "F", "M", "F", "M", "F", "M", "F", "M", "F", "M", "F",
"M", "F", "M", "F", "M", "F", "M", "F", "M", "F", "M", "F", "M",
"F", "M", "F", "M", "F", "M", "F", "M", "F", "M", "F", "M", "F",
"M", "F", "M", "F", "M"), Cases = c(6163L, 9067L, 430L, 1026L,
43L, 67L, 1382L, 888L, 249L, 191L, 278L, 248L, 36L, 2925L, 4248L,
2286L, 1576L, 12106L, 441L, 690L, 7L, 53L, 7645L, 5335L, 10862L,
2546L, 229L, 9136L, 1578L, 2657L, 9301L, 11384L, 27773L, 34435L,
23599L, 32952L, 1105L, 170L, 31L, 94L, 3469L, 2408L, 1L, 6L,
86L, 566L, 1108L, 8355L, 3203L, 7174L, 2314L, 2943L, 46L, 54L,
26L, 187L, 44201L, 837L, 40608L, 38616L, 343L, 12284L, 6571L,
8882L), Deaths = c(24, 136, 4, 51, 0, 1, 43, 26, 4, 10, 0, 2,
1, 242, 84, 112, 49, 1164, 7, 33, 0, 4, 26, 115, 63, 24, 7, 556,
14, 86, 228, 784, 596, 1344, 189, 705, 24, 15, 0, 1, 180, 120,
0, 0, 0, 7, 8, 155, 465, 1630, 39, 125, 3, 3, 0, 0, 2511, 87,
114, 219, 8, 847, 340, 760), Group = c("G1", "G1", "G2", "G2",
"G3", "G3", "G4", "G4", "G5", "G5", "G6", "G6", "G7", "G7", "G8",
"G8", "G9", "G9", "G10", "G10", "G11", "G11", "G12", "G12", "G13",
"G13", "G14", "G14", "G15", "G15", "G16", "G16", "G17", "G17",
"G18", "G18", "G19", "G19", "G20", "G20", "G21", "G21", "G22",
"G22", "G23", "G23", "G24", "G24", "G25", "G25", "G26", "G26",
"G27", "G27", "G28", "G28", "G29", "G29", "G30", "G30", "G31",
"G31", "G32", "G32")), row.names = c(NA, -64L), class = c("tbl_df",
"tbl", "data.frame"))
非常感谢!
最佳答案
我根本不会在这里转。如果您将病例和死亡分开,则对 geom_col
的两次调用将重叠。要获得具有中心 y 轴的镜像设置,我会简单地绘制一个矩形和标签作为注释。这意味着您还需要伪造 x 轴,以调整矩形占用的空间。
您需要将其中一种性别的 x 值设置为负值。由于我们需要对数转换,因此在获取对数后将它们设为负值。
df %>%
mutate(Cases = ifelse(Gender == "F", -log10(Cases) - 0.5, log10(Cases) + 0.5),
Deaths = ifelse(Deaths == 0, 1, Deaths),
Deaths = ifelse(Gender == "F", -log10(Deaths) - 0.5,
log10(Deaths) + 0.5)) %>%
ggplot(aes(Cases, Group, fill = Gender)) +
geom_col(width = 0.8, alpha = 0.2) +
geom_col(width = 0.4, aes(x = Deaths)) +
annotate(geom = "rect", xmin = -0.5, xmax = 0.5, ymax = Inf, ymin = -Inf,
fill = "white") +
annotate(geom = "text", x = 0, y = levels(factor(df$Group)),
label = levels(factor(df$Group))) +
theme_light() +
scale_x_continuous(breaks = c(-4.5, -3.5, -2.5, -1.5, 1.5, 2.5, 3.5, 4.5),
labels = c("10,000", "1,000", "100", "10", "10",
"100", "1,000", "10,000")) +
scale_fill_manual(values = c("#fc7477", "#6f71f2")) +
labs(title = "Female Male",
x = "Cases (Deaths shown in solid bars)") +
theme(axis.text.y = element_blank(),
axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
panel.grid.major.y = element_blank(),
plot.title = element_text(size = 16, hjust = 0.5),
legend.position = "none")
关于r - 使用 ggplot2 在左侧和右侧创建一个带有两个 x 轴的中央 y 轴图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72207178/
我正在使用这段代码: border-right:0px solid #ddd; height:85px; box-shadow :5px 5px 10px 1px #eaeaea; 但我刚刚得到
我遇到的问题是将文本放在图像的左侧... HTML 和 CSS h2, social-hand { font-family: 'PT Sans', sans-serif; display: i
美好的一天 我今天的 CSS 很糟糕。我有 3 列 div(水平,这就是我使用 float 的原因),但我想将我的文本居中对齐,但在包含列 div 的左侧。 HTML:
在我的应用程序中,我在截取屏幕截图时在我的 Imageview 中获得了透明像素。如何剪切顶部、左侧、右侧 和 Imageview 的底部。我尝试了下面的代码但没有工作 Bitmap bitmap =
如何在左侧的以下 LinearLayout 内部添加边框,使其宽度为 5dp 并适合 LinearLayout 的整个高度? LinearLayout 的高度是动态的,这意味着有时它可以是 50dp
我是第一次做网站。我在使用下拉菜单时遇到问题。它正在向左侧移动。请帮助我处理代码,使其位于导航菜单的正下方,并且将鼠标悬停在其上时应该可见。提前致谢。 这是我的 CSS 代码: ul { li
我对 html 和 css 有点生疏,我无法让 2 个图像 float ,一个在另一个之上。这是代码...   &nbs
我有一个固定宽度 (px) 的左侧边栏,右侧是我的主要内容。 这是我的演示:http://jsfiddle.net/fxWg7/4031/ 代码如下: 这是我的 html 代码: left co
所以我正在尝试做一个弹出式菜单,但我遇到了一点问题。 我已经申请了: .menu-side, .menu { -webkit-transition: left 0.2s ease; -
在下面的 CSS @media screen and (max-width: 750px) { .flexbox { display: block; } .menu ul {
@import 'https://fonts.googleapis.com/css?family=PT+Sans'; * { font-family: 'PT Sans', sans-serif
我想在我的标题旁边放一个图标。但它不合适..我怎样才能让它合适? My heading 最佳答案 使用vertical-align:middle,因此如果您更改文本或图像的大小,位
我正在使用参数方程围绕一个圆定位图像 ( How do I calculate a point on a circle’s circumference? ) 我使用简单的数学方法以更少的方式完成此操作
我想使用外部列表属性将 ul 列表正确定位在左浮动 img 的右侧,但元素符号未在图像右侧对齐,因为没有任何图像,但更多在左边。 Lorem ipsum dolor sit amet, c
我有一个layer-list.xml 现在我想在运行时更改 (top,right) 等项目的
我通过按:M-x linum-mode 来启用它们。如何将其“翻译”到 my.emacs 文件中,以便每次打开 EMACS 时自动启用它? 最佳答案 如果您希望每个缓冲区都使用它,请将 (global
我的Code : HTML 1 2 3 CSS #score { height:50px; } .myDiv { width:40px; height:40px;
我可以轻松设置 canvas.width 和 canvas.height 属性。我找不到正确的 JavaScript 语法来使用顶部和左侧设置 Canvas 的偏移量。我尝试过直接在 Canvas 上
我正在尝试为我正在编写的单人纸牌游戏完成条件游戏。 我把所有的 Action 和棋子都移除了。每一 block 都作为椭圆 UI 元素保存在一个二维数组中,当一 block 被拿走时,它会被替换为边框
我正在使用导航 View 修改 Android 应用程序。对于前几个菜单项,我给出了一个图标,但之后,我只给出了菜单标题。但是在导航面板中,没有图标的菜单项有一个空白区域。 如何删除空白区域?我希望菜
我是一名优秀的程序员,十分优秀!