- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想将标签作为百分比添加到我的李克特图表的各个条形中。 是否有捷径可寻? 我试过使用 geom_text,调整排序命令,并启用“plot.percents = TRUE”。这些东西似乎都不起作用。
我的代码、数据和当前图如下。
library(ggplot2)
library(reshape)
library(likert)
library(dplyr)
setwd("~/Desktop/")
df <- read.csv("Likert_Test.csv")
df[2] <- lapply(df[2], as.factor)
colnames(df)[2] <- c("cake?")
df[2] <- lapply(df[2], factor, levels = 1:4)
myplot <- likert(df[2], grouping = df$gender)
plot(myplot, centered = FALSE, col = c("#0A2240", "#3474DA", "#C1A783", "#323A45")) +
scale_y_continuous(labels = function(x) paste0(x, "%")) +
ggtitle("How much do you like...") +
theme(legend.title = element_blank(),
axis.title = element_blank(),
plot.title = element_text(hjust = 0.5),
aspect.ratio = 1/6)
gender cake
Male 3
Male 2
Male 2
Male 4
Male 2
Male 2
Male 2
Male 1
Male 4
Female 1
Female 3
Female 3
Female 3
Female 1
Female 4
Female 4
Female 3
Female 2
Female 3
最佳答案
您在左侧和右侧的百分比定位与您的列值不匹配有一个奇怪的问题。更奇怪的是,使用 Centered = TRUE
,您的不同值已正确排序并且与您的百分比显示相匹配:
plot(myplot, centered = TRUE, ordered = TRUE, col = c("#0A2240", "#3474DA", "#C1A783", "#323A45"))
ggplot2
的解决方案而不是
likert
,您可以从操作 Likert 的结果开始,以便将其转换为更长的格式,如下所示:
library(tidyr)
library(dplyr)
as.data.frame(myplot$results) %>% rowwise() %>%
mutate(Label_Left = sum(`1`,`2`), Label_Right = `3`+`4`) %>%
pivot_longer(cols =`1`:`4`, names_to = "Response", values_to = "Percentage")
# A tibble: 16 x 6
Group Item Label_Left Label_Right Response Percentage
<fct> <fct> <dbl> <dbl> <chr> <dbl>
1 Female cake? 30 70 1 20
2 Female cake? 30 70 2 10
3 Female cake? 30 70 3 50
4 Female cake? 30 70 4 20
5 Female cookies? 70 30 1 50
6 Female cookies? 70 30 2 20
7 Female cookies? 70 30 3 10
8 Female cookies? 70 30 4 20
9 Male cake? 66.7 33.3 1 11.1
10 Male cake? 66.7 33.3 2 55.6
11 Male cake? 66.7 33.3 3 11.1
12 Male cake? 66.7 33.3 4 22.2
13 Male cookies? 66.7 33.3 1 22.2
14 Male cookies? 66.7 33.3 2 44.4
15 Male cookies? 66.7 33.3 3 33.3
16 Male cookies? 66.7 33.3 4 0
ggplot
部分和使用
geom_text
显示所有需要的标签:
library(tidyr)
library(dplyr)
library(ggplot2)
as.data.frame(myplot$results) %>% rowwise() %>%
mutate(Label_Left = sum(`1`,`2`), Label_Right = `3`+`4`) %>%
pivot_longer(cols =`1`:`4`, names_to = "Response", values_to = "Percentage") %>%
ggplot(aes(x = Group, y = Percentage))+
geom_col(aes(fill = Response), position = position_stack(reverse = TRUE))+
geom_text(data = . %>% filter(Percentage !=0),
aes(fill = Response, label = scales::percent(Percentage/100)),
position = position_stack(reverse = TRUE,0.5), color = "white")+
scale_fill_manual(values = c("#0A2240", "#3474DA", "#C1A783", "#323A45"))+
scale_y_continuous(labels = function(x) scales::percent(x/100), name = "")+
coord_flip()+
facet_wrap(~Item, ncol = 1)+
geom_text(data = . %>% distinct(Group, Item, Label_Left),
aes(x = Group, y = 0, label = scales::percent(round(Label_Left,0)/100)), hjust = 1)+
geom_text(data = . %>% distinct(Group, Item, Label_Right),
aes(x = Group, y = 100, label = scales::percent(round(Label_Right,0)/100)), hjust = 0)+
theme(legend.position = "bottom",
axis.title.y = element_blank())+
labs(title = "How much do you like... ")
df <- data.frame(gender = c(rep(c("Male","Female"), each = 9),"Female"),
cake = c(3,2,2,4,2,2,2,1,4,1,3,3,3,1,4,4,3,2,3),
cookie = c(1,2,2,2,3,3,3,1,2,1,1,4,4,1,3,2,2,1,1))
关于r - 在 Likert 中的单个条形顶部叠加百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61369850/
我的函数概念上都返回相同的东西,但结果可以采用不同的形式: function GetThingy() 有四个不同的函数,每个可以返回不同的东西: 0.071(代表增长 7.1% 的 float 值)
这个问题在这里已经有了答案: Int division: Why is the result of 1/3 == 0? (19 个回答) 关闭 4 年前。 有什么方法可以计算(例如)120 的 50
我四处寻找这个,它看起来很简单,但我无法让它工作。 我有一张表格,其中一列需要格式化为百分比。下面是我的代码,但它没有格式化单元格,它只是将它们保留为小数。 我想这是因为 cell ,即使声明为范围,
我刚刚开始使用 WPF。从那以后,我开始关注造型系统。我来自 CSS 背景,我想以百分比设置边距。 当前值以像素为单位
我有一个表,其中每一行都有一个描述字段和一个 bool 值。我正在尝试编写一个查询,我可以在其中按每个相应的描述进行分组,并查看 bool 值为真的次数百分比。 示例表: PID Gen
我从文档中发现,考虑到 orientdb 100% 使用磁盘缓存,它使用的最大大小为 70% 用于读取缓存,30% 用于写入缓存 ( http://orientdb.com/docs/last/plo
有什么方法可以获取 docker 容器内部而不是外部的 cpu 百分比吗?! docker stats DOCKER_ID 显示的百分比正是我需要的,但我需要它作为变量。我需要获取容器本身内部的 cp
我正在尝试计算数据集每列中类别的比例(百分比)。 示例数据: df <- data.frame( "Size" = c("Y","N","N","Y","Y"), "Type" =
我应该使用小数还是 float 在数据库中存储比率?特别是在 SQL2005 中。 最佳答案 这取决于您对准确性的需求。如果您可以容忍来自存储 float 的 IEEE 方法的典型错误,则使用 flo
我正在创建一个游戏,目前必须处理一些math.random问题。 我的Lua能力不是那么强,你觉得怎么样 您能制定一个使用 math.random 和给定百分比的算法吗? 我的意思是这样的函数: fu
如何在SQL中动态计算百分比? 假设您有一个名为 Classes 的下表: ClassSession StudentName -------------------------------
如何通过 jQuery 创建具有百分比的数字掩码输入?我是否让输入仅接受三个数字,并在用户完成输入时在数字后添加百分号(keyup)? 我不使用插件。 示例:1% 或 30% 或 99% 或 100%
我正在尝试构建一个工具,可以突出显示具有最高介数中心性的社交网络节点。我将所有网络节点的测量值计算到字典中,按顺序对字典进行排序,然后仅保留前 3 对。 我希望这个工具是可扩展的,所以我想保留前 10
MYSQL 中的人员如何将一个日期条目和分数的用户百分比与另一个日期条目和分数进行比较,从而有效地返回从一个日期到另一个日期的用户百分比增加情况? 几天来我一直在试图解决这个问题,但我已经没有想法了,
我需要进行查询,结果是百分比。 我现在的查询如下所示: select COUNT(CREATE_WEEKDAY), CREATE_WEEKDAY, COUNT(CREATE
我有一个图像上传功能,其工作原理如下: $('.update-insertimage-form').submit(function() { $(".submit-newupdate-btn").add
我的问题很简单,但我仍然找不到这个问题的答案... 假设我们有两个包含图像的容器。 我们有类似的东西 #containera { width: 50%; height: 50%; backgr
是否可以将 CSS 尺寸指定为除其父元素之外的另一个元素的百分比?例如,我想将 div 的 border-radius 指定为 div 宽度的 10%。但是,border-radius: 10% 在
我正在尝试设置按钮的大小并以百分比进行编辑 但是这个的线性大小是不同的。为什么? 最佳答案 您好,问题出在属性 box-sizing 上.默认为 input type
我将它用于我的页眉,该页眉在一页上下滚动页面中发生变化。我注意到它没有响应,所以我想问你是否知道一种使它响应的方法。就像将 0-690 更改为百分比,以便它可以在移动设备和电视屏幕上使用。 HTML
我是一名优秀的程序员,十分优秀!