问题描述
在上一篇文章中,用户向我展示了如何在R中绘制纵向数据.这是代码:
库(ggplot2)数据<-data.frame("ID"= c("ABC111","ABC111","ABC111","ABC111","ABC112","ABC112","ABC112","ABC113","ABC113","ABC114","ABC115"),颜色"= c(红色",红色",红色",红色",蓝色",蓝色",蓝色",绿色",绿色",蓝色").黑色",黄色"),开始日期"= c("2005/01/01","2006/01/01","2007/01/01","2008/01/01","2009/01/01","2010/01/01","2011/01/01","2012/01/01","2013/01/01","2014/01/01","2015/01/01";)," end_date"= c("2005/09/01","2006/06/01","2007/04/01","2008/05/07","2009/06/01","2010/10/01","2011/12/12","2013/05/01","2013/06/08","2015/01/01","2016/08/09";))Data $ ID = as.factor(数据$ ID)Data $ color = as.factor(数据$ color)图书馆(tidyverse)数据%>%#按显示顺序对每一行编号,#将此数字保存在名为order的新列中rowid_to_column("order")%&%#将数据从宽格式更改为长格式ivot_longer(cols = c(开始日期,结束日期),names_to ="date_type",values_to ="date")%>%#Ggplot,使用日期作为x,使用订单作为y,使用ID作为列,使用订单作为组ggplot(aes(x = date,y =阶,col = ID,组=订单))+#画点geom_point()+# 画线geom_line()+#也许您想删除y轴的标题,文本和刻度主题(axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),#我向x轴标签添加了垂直格式#这样阅读可能会更容易axis.text.x = element_text(角度= 90,垂直= 0.5))
该解决方案需要"tidyverse"标记.图书馆.我正在工作的计算机没有USB端口或Internet连接,它只有安装了一些软件包的R(例如dplyr,ggplot2,reshape2).可以制作此代码,以便使用"dplyr"代替"tidyverse".和"reshape2"?
我尝试了以下代码(在上一篇文章中建议给我):
数据%>%#按显示顺序对每一行编号,#将此数字保存在名为order的新列中mutate(order = row_number())%&%;%#将数据从宽格式更改为长格式融化(cols = c(开始日期,结束日期),names_to ="date_type",values_to ="date")%>%#Ggplot,使用日期作为x,使用订单作为y,使用ID作为列,使用订单作为组ggplot(aes(x = date,y =阶,col = ID,组=订单))+#画点geom_point()+# 画线geom_line()+#也许您想删除y轴的标题,文本和刻度主题(axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),#我向x轴标签添加了垂直格式#这样阅读可能会更容易axis.text.x = element_text(角度= 90,垂直= 0.5))
但是我遇到了以下错误:
使用ID,颜色,开始日期,结束日期作为ID变量不知道如何为类型函数的对象自动选择比例.默认为连续.不知道如何为类型函数的对象自动选择比例.默认为连续.不知道如何为类型函数的对象自动选择比例.默认为连续.错误:美学必须是有效的数据列.问题美学:x =日期,y =顺序,组=顺序.
有人可以告诉我我在做什么错吗?
谢谢
链接到上一篇文章:
In a previous post, a user showed me how to to plot longitudinal data in R. Here is the code:
library(ggplot2)
Data <- data.frame(
"ID" = c("ABC111", "ABC111", "ABC111", "ABC111", "ABC112", "ABC112", "ABC112", "ABC113", "ABC113", "ABC114", "ABC115"),
"color" = c("red", "red", "red", "red", "blue", "blue", "blue", "green", "green", "black", "yellow"),
"start_date" = c("2005/01/01", "2006/01/01", "2007/01/01", "2008/01/01", "2009/01/01", "2010/01/01", "2011/01/01", "2012/01/01", "2013/01/01", "2014/01/01", "2015/01/01"),
"end_date" = c("2005/09/01", "2006/06/01", "2007/04/01", "2008/05/07", "2009/06/01", "2010/10/01", "2011/12/12", "2013/05/01", "2013/06/08", "2015/01/01", "2016/08/09")
)
Data$ID = as.factor(Data$ID)
Data$color = as.factor(Data$color)
library(tidyverse)
Data %>%
# Number each row in its order of appearance,
# save this numbers in a new column named order
rowid_to_column("order") %>%
# Change data from wide to long format
pivot_longer(cols = c(start_date, end_date),
names_to = "date_type",
values_to = "date") %>%
# Ggplot, use date as x, order as y, ID as col and order as group
ggplot(aes(x = date,
y = order,
col = ID,
group = order)) +
# Draw points
geom_point()+
# Draw lines
geom_line() +
# Maybe you want to remove the y axis title, text and ticks
theme(axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
# I added a vertical format to the x axis labels
# it might easier to read this way
axis.text.x = element_text(angle = 90, vjust = 0.5))
This solution requires the "tidyverse" library. The computer I am using for work does not have a USB port or an internet connection, it only has R with a few packages installed (such as dplyr, ggplot2, reshape2). Can this code be made so that instead of "tidyverse", it uses "dplyr" and "reshape2"?
I tried the following code (suggested to me in a previous post):
Data %>%
# Number each row in its order of appearance,
# save this numbers in a new column named order
mutate(order = row_number()) %>%
# Change data from wide to long format
melt(cols = c(start_date, end_date),
names_to = "date_type",
values_to = "date") %>%
# Ggplot, use date as x, order as y, ID as col and order as group
ggplot(aes(x = date,
y = order,
col = ID,
group = order)) +
# Draw points
geom_point()+
# Draw lines
geom_line() +
# Maybe you want to remove the y axis title, text and ticks
theme(axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
# I added a vertical format to the x axis labels
# it might easier to read this way
axis.text.x = element_text(angle = 90, vjust = 0.5))
But I got the following error:
Using ID, color, start_date, end_date as id variables
Don't know how to automatically pick scale for object of type function. Defaulting to continuous.
Don't know how to automatically pick scale for object of type function. Defaulting to continuous.
Don't know how to automatically pick scale for object of type function. Defaulting to continuous.
Error: Aesthetics must be valid data columns. Problematic aesthetic(s): x = date, y = order, group = order.
Can someone please tell me what I am doing wrong?
Thanks
Link to previous post: R: connect points on a graph (ggplot2)
Using only melt()
from reshape2
as suggested by great comment of @qdread and some base R
:
library(reshape2)
library(ggplot2)
#Code
Data$order <- rownames(Data)
Melted <- melt(Data,id.vars = c('order','ID','color'))
Melted$order <- as.numeric(Melted$order)
#Plot
G <- ggplot(Melted,aes(x = value,
y = order,
col = ID,
group = order)) +
# Draw points
geom_point()+
# Draw lines
geom_line() +
# Maybe you want to remove the y axis title, text and ticks
theme(axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
# I added a vertical format to the x axis labels
# it might easier to read this way
axis.text.x = element_text(angle = 90, vjust = 0.5))
Output:
这篇关于R:将tidyverse转换为dplyr/reshape2以进行绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!