R 根据第三列中的事件更改图表上点的颜色
我正在根据谷歌分析数据创建一个简单的折线图,跟踪每天的用户数量。我在线形图上加了一个点来标记每个日期。代码看起来像这样R 根据第三列中的事件更改图表上点的颜色,r,ggplot2,R,Ggplot2,我正在根据谷歌分析数据创建一个简单的折线图,跟踪每天的用户数量。我在线形图上加了一个点来标记每个日期。代码看起来像这样 ggplot(web_visit_vs_email_deployed.df,aes(x = date, y = users))+ geom_line()+ geom_point(aes(color = !is.na(Program)))+ theme_tq() + labs( title = "Website Visits vs. Emails Deployed"
ggplot(web_visit_vs_email_deployed.df,aes(x = date, y = users))+
geom_line()+
geom_point(aes(color = !is.na(Program)))+
theme_tq() +
labs(
title = "Website Visits vs. Emails Deployed",
x = "",
y = "Users",
color = "Email Deployed"
)
我想更改我们部署营销电子邮件日期点的颜色。我将用于绘制上述内容的数据框与另一个包含电子邮件性能指标的数据框中的“电子邮件日期”列连接起来。结果如下表所示
date users Program
<dttm> <dbl> <chr>
1 2020-01-01 00:00:00 80 NA
2 2020-01-02 00:00:00 183 NA
3 2020-01-03 00:00:00 176 NA
4 2020-01-04 00:00:00 86 NA
5 2020-01-05 00:00:00 87 NA
6 2020-01-06 00:00:00 164 NA
7 2020-01-07 00:00:00 177 NA
8 2020-01-08 00:00:00 136 NA
9 2020-01-09 00:00:00 515 HEA
10 2020-01-10 00:00:00 231 NA
# ... with 53 more rows
日期用户程序
1202-01-01 00:00:00 80北美
2020-01-02 00:00:00 183北美
32020-01-0300:00:00176北美
4 2020-01-04 00:00:00 86北美
52020-01-0500:00:0087北美
6 2020-01-06 00:00:00 164北美
7 2020-01-07 00:00:00 177北美
8 2020-01-08 00:00:00 136北美
9 2020-01-09 00:00:00 515 HEA
102020-01-10200:00:00231北美
# ... 还有53行
现在,基于两种不同颜色的“程序”列“NA”或“HEA”,可以创建两个单独的折线图。相反,我希望一行有不同颜色的点,基于“程序”列为“NA”或“HEA”
编辑:更新的绘图和数据框
编辑2:做了更多的游戏并解决了它。谢谢大家的帮助 我认为最好的解决方案是创建一个虚拟变量列,其中1表示您确实发送了电子邮件,0表示您没有发送电子邮件。这样,虚拟变量列就可以用于颜色美学
library(dplyr)
library(ggplot2)
web_visit_by_date.df = data.frame('date' = c('2020-01-01','2020-01-02','2020-01-03'), 'users' = c(80, 183, 176)) # replicating your data snippet
email_dates = c('2020-01-01', '2020-01-03') # the join didn't make sense to me, so I left this as a list
# making the dummy variable using ifelse. The argument is checking to see if the
# date column contains a value from the email_dates list. If yes, then that row
# equals true and gets a 1 in the new email column. Otherwise, that row equals
# false and gets a 0 in the new email column.
web_visit_by_date.df$email = ifelse((c(web_visit_by_date.df$date %in% email_dates) == TRUE), 1, 0)
# all that's left is to set color = as.factor(email) in the aes argument
ggplot(web_visit_by_date.df,aes(x = date, y = users, color = as.factor(email)))+
geom_line()+
geom_point()+
labs(
title = "Website Visits vs. Emails Deployed",
x = "",
y = "Users"
)
我认为最好的解决方案是创建一个虚拟变量列,其中1表示您确实发送了电子邮件,0表示您没有。这样,虚拟变量列就可以用于颜色美学
library(dplyr)
library(ggplot2)
web_visit_by_date.df = data.frame('date' = c('2020-01-01','2020-01-02','2020-01-03'), 'users' = c(80, 183, 176)) # replicating your data snippet
email_dates = c('2020-01-01', '2020-01-03') # the join didn't make sense to me, so I left this as a list
# making the dummy variable using ifelse. The argument is checking to see if the
# date column contains a value from the email_dates list. If yes, then that row
# equals true and gets a 1 in the new email column. Otherwise, that row equals
# false and gets a 0 in the new email column.
web_visit_by_date.df$email = ifelse((c(web_visit_by_date.df$date %in% email_dates) == TRUE), 1, 0)
# all that's left is to set color = as.factor(email) in the aes argument
ggplot(web_visit_by_date.df,aes(x = date, y = users, color = as.factor(email)))+
geom_line()+
geom_point()+
labs(
title = "Website Visits vs. Emails Deployed",
x = "",
y = "Users"
)
简单的解决方案是在现有绘图的顶部绘制电子邮件日期,方法是在原始代码的第3行之后添加这一行:
geom_point(aes(x=`email Date`)、color='red',size=2)+
,这会在黑色小点的顶部生成较大的红色点。这只是给了我一个完全空白的情节。我相信这个问题来自于我的数据框中的“Email Date”列中的值,其中我有“user”值,反之亦然。一旦我得到排序,我喜欢这个解决方案!简单的解决方案是在现有绘图的顶部绘制电子邮件日期,方法是在原始代码的第3行之后添加这一行:geom_point(aes(x=`email Date`)、color='red',size=2)+
,这会在黑色小点的顶部生成较大的红色点。这只是给了我一个完全空白的情节。我相信这个问题来自于我的数据框中的“Email Date”列中的值,其中我有“user”值,反之亦然。一旦我得到排序,我喜欢这个解决方案!谢谢我相信这个解决方案会起作用,但正如你们提到的,我将两个数据帧连接起来并没有什么意义。我找到了一种通过“日期”加入他们的方法,在我发送电子邮件的日子里,在“日期”栏中给我提供了重复项,在“用户”栏中,每个重复的“日期”的一个实例上都有NA值。不过,很快就要弄明白了。以我认为有效的方式设计了数据帧连接。如果您能提供帮助,请编辑原始帖子,并提供绘图和数据框的新输出!谢谢我相信这个解决方案会起作用,但正如你们提到的,我将两个数据帧连接起来并没有什么意义。我找到了一种通过“日期”加入他们的方法,在我发送电子邮件的日子里,在“日期”栏中给我提供了重复项,在“用户”栏中,每个重复的“日期”的一个实例上都有NA值。不过,很快就要弄明白了。以我认为有效的方式设计了数据帧连接。如果您能提供帮助,请编辑原始帖子,并提供绘图和数据框的新输出!