用我的数据:
Row | Year | SchoolID | SchoolName | BudgetArea |PaymentPerStudent
001 2011 ABC PS #1 Staff 12000
002 2012 ABC PS #1 Staff 10000
003 2011 ABC PS #1 Lunch 22000
004 2012 ABC PS #1 Lunch 18000
005 2011 DEF PS #2 Staff 80000
006 2012 DEF PS #2 Staff 65000
007 2013 DEF PS #2 Staff 50000
008 2011 DEF PS #2 Lunch 23000
009 2012 DEF PS #2 Lunch 34000
010 2013 DEF PS #2 Lunch …
Run Code Online (Sandbox Code Playgroud) 我的目的是遮蔽密度曲线下面两个点之间的区域.在这个例子中,我想遮蔽值.25和.5之间的区域.
我已经能够用以下方法绘制我的密度曲线:
setwd("D:/Workspace")
# -- create dataframe
coursename <- c('Math','Math','Math','Math','Math')
value <- c(.12, .4, .5, .8, .9)
df <- data.frame(coursename, value)
library(ggplot2)
density_plot <- ggplot(aes(x=value, colour=coursename, fill=coursename), data=df) +
geom_density(alpha=.3) +
geom_vline(aes(xintercept=.5), colour="blue", data=df, linetype="dashed", size=1) +
scale_x_continuous(breaks=c(0, .25, .5, .75, 1), labels=c("0", ".25", ".5", ".75", "1")) +
coord_cartesian(xlim = c(0.01, 1.01)) +
theme(axis.title.y=element_blank(), axis.text.y=element_blank()) +
ggtitle("sample data")
density_plot
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下代码来遮蔽.25和.5之间的区域:
x1 <- min(which(df$value >=.25))
x2 <- max(which(df$value <=.5))
with(density_plot, polygon(x=c(x[c(x1,x1:x2,x2)]), y=c(0, y[x1:x2], 0), col="gray"))
Run Code Online (Sandbox Code Playgroud)
但它只会生成以下错误:
Error in xy.coords(x, y) …
Run Code Online (Sandbox Code Playgroud) 我正在处理调查数据。调查项目有两组,每组三个项目。我的调查样本中有两名受访者。
我正在尝试按调查项目组生成热图,其中:
这是一个完全可重现的示例:
wd <- "D:/Desktop/"
setwd(wd)
#--create dataframe
respondent = c("Respondent_1", "Respondent_1", "Respondent_1","Respondent_1", "Respondent_1", "Respondent_1",
"Respondent_2", "Respondent_2", "Respondent_2","Respondent_2", "Respondent_2", "Respondent_2")
item = c("Item_1", "Item_2", "Item_3","Item_1", "Item_2", "Item_3",
"Item_1", "Item_2", "Item_3","Item_1", "Item_2", "Item_3")
item_group = c("Group_1","Group_1","Group_1","Group_2","Group_2","Group_2",
"Group_1","Group_1","Group_1","Group_2","Group_2","Group_2")
score = c(1, 40, 100, 100, 30, 12,
2, 15, 80, 77, 44, 10)
high_value_color = c("darkred", "darkred", "darkred",
"brown3", "brown3", "brown3")
plot_df = data.frame(respondent, item, item_group, score, high_value_color)
#--write function
#--inspired from this: http://www.reed.edu/data-at-reed/resources/R/loops_with_ggplot2.html
plot_list <- unique(plot_df$item_group)
survey_items.graph <- function(df, …
Run Code Online (Sandbox Code Playgroud)