繁体   English   中英

为什么数据条没有显示在这个 R 图中?

[英]Why are data bars not showing up in this R plot?

我有一些代码来创建一个发散堆积条形图。 我修改了它,它运行没有错误。 但是,该图与预期不同——数据条在保存的图中未正确显示。 我对 R 比较陌生,不知道如何解决这个问题......

library(devtools)
library(likert)
#library(plyr)
#library(reshape2)

scale_height = knitr::opts_chunk$get('fig.height')*0.5
scale_width = knitr::opts_chunk$get('fig.width')*1.25
knitr::opts_chunk$set(fig.height = scale_height, fig.width = scale_width)

theme_update(legend.text = element_text(size = rel(0.7)))
theme_update(plot.title = element_text(hjust = 0.5))


title = "TITLE"

headers = c("Item", "Strongly Disagree", "Disagree", "Neutral", "Agree", "Strongly Agree")

x_label = "Category"

data_csv_text = "Type   Strongly Disagree   Disagree    Neutral Agree   Strongly Agree
Ask weather-related questions (e.g. temperature, need umbrella) 0   0.05    0   0.3 0.65
Set alarms, events, and reminders   0   0.05    0.1 0.25    0.6
Set timers  0   0   0.2 0.4 0.4
Get alerts (e.g. doorbells, smoke alarms)   0   0   0.05    0.35    0.6
Search for information (e.g. recipes, movie times)  0   0.05    0.05    0.2 0.7
Connect to other smart devices (e.g. lights, TV, cars)  0.05    0   0.05    0.5 0.4
Video-based communication (e.g. videophone/VRS) 0   0.05    0.1 0.55    0.3
Notifications (e.g. read, delete notifications) 0   0.05    0.35    0.45    0.15
Information, Warnings (e.g. traffic, weather conditions)    0   0   0.05    0.5 0.45
Manage notes (e.g. to-do lists, shopping lists) 0   0   0.4 0.4 0.2"

location = "<REDACTED_FOR_PRIVACY>"


data = read.csv(text=data_csv_text, header=TRUE, sep="\t")
data$Type = as.factor(data$Type)
names(data) = headers
data_summary = likert(summary = data)
plot = plot(data_summary, plot.percents=TRUE, plot.percent.low=FALSE, plot.percent.high=FALSE) + xlab(x_label) + ggtitle(title)

plot$layers[[2]]$geom_params$width = 0.5
plot$layers[[3]]$geom_params$width = 0.5

cowplot::save_plot(paste(location, "plot.png", sep=""), plot,base_width=300,base_height=65, units="mm",ncol = 1, nrow = 1)

这会在我的机器上生成以下图: 在此处输入图片说明


数据

# dput(data)
data <- structure(list(Item = structure(c(1L, 8L, 9L, 3L, 7L, 2L, 10L, 6L, 4L, 5L), .Label = c("    Ask weather-related questions (e.g. temperature, need umbrella)", "    Connect to other smart devices (e.g. lights, TV, cars)", "    Get alerts (e.g. doorbells, smoke alarms)", "    Information, Warnings (e.g. traffic, weather conditions)", "    Manage notes (e.g. to-do lists, shopping lists)", "    Notifications (e.g. read, delete notifications)", "    Search for information (e.g. recipes, movie times)", "    Set alarms, events, and reminders", "    Set timers", "    Video-based communication (e.g. videophone/VRS)"), class = "factor"), `Strongly Disagree` = c(0, 0, 0, 0, 0, 0.05, 0, 0, 0, 0), Disagree = c(0.05, 0.05, 0, 0, 0.05, 0, 0.05, 0.05, 0, 0), Neutral = c(0, 0.1, 0.2, 0.05, 0.05, 0.05, 0.1, 0.35, 0.05, 0.4), Agree = c(0.3, 0.25, 0.4, 0.35, 0.2, 0.5, 0.55, 0.45, 0.5, 0.4), `Strongly Agree` = c(0.65, 0.6, 0.4, 0.6, 0.7, 0.4, 0.3, 0.15, 0.45, 0.2)), row.names = c(NA, -10L), class = "data.frame")

...我意识到likert汇总数据必须以百分比表示,而不是小数...

所以,更换

data_csv_text = "Type   Strongly Disagree   Disagree    Neutral Agree   Strongly Agree
Ask weather-related questions (e.g. temperature, need umbrella) 0   0.05    0   0.3 0.65
Set alarms, events, and reminders   0   0.05    0.1 0.25    0.6
Set timers  0   0   0.2 0.4 0.4
Get alerts (e.g. doorbells, smoke alarms)   0   0   0.05    0.35    0.6
Search for information (e.g. recipes, movie times)  0   0.05    0.05    0.2 0.7
Connect to other smart devices (e.g. lights, TV, cars)  0.05    0   0.05    0.5 0.4
Video-based communication (e.g. videophone/VRS) 0   0.05    0.1 0.55    0.3
Notifications (e.g. read, delete notifications) 0   0.05    0.35    0.45    0.15
Information, Warnings (e.g. traffic, weather conditions)    0   0   0.05    0.5 0.45
Manage notes (e.g. to-do lists, shopping lists) 0   0   0.4 0.4 0.2"

data_csv_text = "Type   Strongly Disagree   Disagree    Neutral Agree   Strongly Agree
Ask weather-related questions (e.g. temperature, need umbrella) 0   5   0   30  65
Set alarms, events, and reminders   0   5   10  25  60
Set timers  0   0   20  40  40
Get alerts (e.g. doorbells, smoke alarms)   0   0   5   35  60
Search for information (e.g. recipes, movie times)  0   5   5   20  70
Connect to other smart devices (e.g. lights, TV, cars)  5   0   5   50  40
Video-based communication (e.g. videophone/VRS) 0   5   10  55  30
Notifications (e.g. read, delete notifications) 0   5   35  45  15
Information, Warnings (e.g. traffic, weather conditions)    0   0   5   50  45
Manage notes (e.g. to-do lists, shopping lists) 0   0   40  40  20"

成功了!

现在我得到这个输出: 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM