我的目标的第一步实现了(谢谢@akrun),但我似乎无法跨越球门线!继续我的第一个问题得到了很快的回答,我仍然无法创建直方图。
这是我希望动态输出看起来像的一个工作示例:
library(dplyr)
library(ggplot2)
library(tidyr)
df <- data.frame(timeline = Sys.Date() - 1:10,
q3 = sample(c("Yes", "No"), size = 10, replace = T),
q4 = sample(c("Yes", "No"), size = 10, replace = T),
q5 = sample(c("Yes", "No"), size = 10, replace = T),
q6 = sample(c("Yes", "No"), size = 10, replace = T),
q7 = sample(c("Yes", "No"), size = 10, replace = T),
q8 = sample(c("Yes", "No"), size = 10, replace = T),
stringsAsFactors = F) %>%
mutate(q3 = ifelse(q3 == "Yes", 1, 0),
q4 = ifelse(q4 == "Yes", 1, 0),
q5 = ifelse(q5 == "Yes", 1, 0),
q6 = ifelse(q6 == "Yes", 1, 0),
q7 = ifelse(q7 == "Yes", 1, 0),
q8 = ifelse(q8 == "Yes", 1, 0)
) %>%
gather(key = question, value = value, q3, q4, q5, q6, q7, q8)
g <- ggplot(df, aes(x = timeline, y = value, fill = question)) +
geom_bar(stat = "identity")
g
这就是我在help之后到达的地方。这是创建的数据框的示例。我不知道列数,名称或观察数量,因此必须动态工作。
Appetite <- c("No","Yes","No","No","No","No","No","No","No")
Dental.Health <- c("No","Yes","No","No","No","No","Yes","Yes","No")
Dry.mouth <- c("No","Yes","Yes","Yes","Yes","No","Yes","Yes","No")
Mouth.opening <- c("No","No","Yes","Yes","Yes","No","Yes","Yes","No")
Pain.elsewhere <- c("No","Yes","No","No","No","No","No","No","No")
Sleeping <- c("No","No","No","No","No","Yes","No","No","No")
Sore.mouth <- c("No","No","Yes","Yes","No","No","No","No","No")
Swallowing <- c("No","No","No","No","Yes","No","No","No","No")
Cancer.treatment <- c("No","No","Yes","Yes","No","Yes","No","No","No")
Support.for.my.family <- c("No","No","Yes","Yes","No","No","No","No","No")
Fear.of.cancer.coming.back <- c("No","No","Yes","Yes","No","No","Yes","No","No")
Intimacy <- c("Yes","No","No","No","No","No","No","No","No")
Dentist <- c("No","Yes","No","No","No","No","No","No","No")
Dietician <- c("No","No","Yes","Yes","No","No","No","No","No")
Date.submitted <- c("2002-07-25 00:00:00",
"2002-09-05 00:00:00",
"2003-01-09 00:00:00",
"2003-01-09 00:00:00",
"2003-07-17 00:00:00",
"2003-11-06 00:00:00",
"2004-12-17 00:00:00",
"2005-06-03 00:00:00",
"2005-12-17 00:00:00")
theDates <- as.Date(Date.submitted, "%Y-%m-%d %T")
theDataFrame <- data.frame( timeline = as.list(theDates),
Appetite,
Dental.Health,
Dry.mouth,
Mouth.opening,
Pain.elsewhere,
Sleeping,
Sore.mouth,
Swallowing,
Cancer.treatment,
Support.for.my.family,
Fear.of.cancer.coming.back,
Intimacy,
Dentist,
Dietician,
stringsAsFactors = F) %>%
theDataFrame[-1] <- lapply(theDataFrame[-1], function(x) as.integer(x=="Yes")) %>%
gather(key = question, value = value, as.list(theDataFrame))
go <- ggplot(theDataFrame, aes(x = timeline, y = value, fill = question)) +
geom_bar(stat = "identity")
go
我可能误解了聚集是如何运作的,但我似乎无法理解它。
答案 0 :(得分:0)
In his comment,OP要求让第二个例子起作用。这可以通过对OP代码的一些修改来完成:
library(tidyr)
library(ggplot2)
Appetite <- c("No","Yes","No","No","No","No","No","No","No")
Dental.Health <- c("No","Yes","No","No","No","No","Yes","Yes","No")
Dry.mouth <- c("No","Yes","Yes","Yes","Yes","No","Yes","Yes","No")
Mouth.opening <- c("No","No","Yes","Yes","Yes","No","Yes","Yes","No")
Pain.elsewhere <- c("No","Yes","No","No","No","No","No","No","No")
Sleeping <- c("No","No","No","No","No","Yes","No","No","No")
Sore.mouth <- c("No","No","Yes","Yes","No","No","No","No","No")
Swallowing <- c("No","No","No","No","Yes","No","No","No","No")
Cancer.treatment <- c("No","No","Yes","Yes","No","Yes","No","No","No")
Support.for.my.family <- c("No","No","Yes","Yes","No","No","No","No","No")
Fear.of.cancer.coming.back <- c("No","No","Yes","Yes","No","No","Yes","No","No")
Intimacy <- c("Yes","No","No","No","No","No","No","No","No")
Dentist <- c("No","Yes","No","No","No","No","No","No","No")
Dietician <- c("No","No","Yes","Yes","No","No","No","No","No")
Date.submitted <-
c("2002-07-25 00:00:00", "2002-09-05 00:00:00", "2003-01-09 00:00:00",
"2003-01-09 00:00:00", "2003-07-17 00:00:00", "2003-11-06 00:00:00",
"2004-12-17 00:00:00", "2005-06-03 00:00:00", "2005-12-17 00:00:00")
theDataFrame <- data.frame(
timeline = as.Date(Date.submitted),
Appetite, Dental.Health, Dry.mouth, Mouth.opening, Pain.elsewhere,
Sleeping, Sore.mouth, Swallowing, Cancer.treatment, Support.for.my.family,
Fear.of.cancer.coming.back, Intimacy, Dentist, Dietician,
stringsAsFactors = FALSE)
theDataFrame[-1] <- lapply(theDataFrame[-1], function(x) as.integer(x=="Yes"))
theDataFrame <- theDataFrame %>% gather(question, value, -timeline)
go <- ggplot(theDataFrame, aes(x = timeline, y = value, fill = question)) +
geom_col()
go