R中绘图的轴
axis from plotly chart in R
我正在尝试用 R ( the chart I want) 中的 plotly
重现此甘特图。我有一个包含 6 列的数据框,我想在 y 轴上显示文本,在 x 轴上显示月份和年份。根据我的 dataframe
我有以下内容:
one=c('bla','bla','bla',
'bla','bla','bla','bla','bla','bla','bla',
'bla','bla')
two=c('09/25/2017','10/02/2017','11/15/2017','11/29/2017','01/01/2018','01/01/2018','04/01/2018','07/01/2018','09/01/2018','09/01/2018',
'08/01/2020','09/01/2020')
three=c(1102,55,46,214,181,181,122,62,700,700,31,30)
four=c('bla','bla','bla',
'bla','bla','bla','bla',
'bla','bla','bla'
,'bla','bla')
five=c('A','B','C','D','E','F','G','H','E','I','J','E')
df=data.frame(one,two,three,four,five)
df$two =as.Date(df$two,"%m/%d/%Y")
client = "my example"
# Choose colors based on number of resources
cols <- RColorBrewer::brewer.pal(length(unique(df$five)), name = "Set3")
df$color <- factor(df$five, labels = cols)
# Initialize empty plot
p <- plot_ly()
# Each task is a separate trace
# Each trace is essentially a thick line plot
# x-axis ticks are dates and handled automatically
for(i in 1:(nrow(df))){
p <- add_trace(p,
x = c(df$two[i], df$two[i] + df$three[i]), # x0, x1
y = c(i, i), # y0, y1
mode = "lines",
line = list(color = df$color[i], width = 20),
showlegend = F,
hoverinfo = "text",
# Create custom hover text
text = paste("Task: ", df$one[i], "<br>",
"Duration: ", df$three[i], "days<br>",
"Resource: ", df$five[i]),
evaluate = T # needed to avoid lazy loading
)
}
# Add information to plot and make the chart more presentable
p <- layout(p,
# Axis options:
# 1. Remove gridlines
# 2. Customize y-axis tick labels and show task names instead of numbers
xaxis = list(showgrid = F, tickfont = list(color = "#e6e6e6")),
yaxis = list(showgrid = F, tickfont = list(color = "#e6e6e6"),
tickmode = "array", tickvals = 1:nrow(df), ticktext = unique(df$one),
domain = c(0, 0.9)),
# Annotations
annotations = list(
# Add total duration and total resources used
# x and y coordinates are based on a domain of [0,1] and not
# actual x-axis and y-axis values
list(xref = "paper", yref = "paper",
x = 0.80, y = 0.1,
text = paste0("Total Duration: ", sum(df$three), " days<br>",
"Total Resources: ", length(unique(df$five)), "<br>"),
font = list(color = "#ffff66", size = 12),
ax = 0, ay = 0,
align = "left"),
# Add client name and title on top
list(xref = "paper", yref = "paper",
x = 0.1, y = 1, xanchor = "left",
text = paste0("Gantt Chart: ", client),
font = list(color = "#f2f2f2", size = 20, family = "Times New Roman"),
ax = 0, ay = 0,
align = "left")
),
plot_bgcolor = "#333333", # Chart area color
paper_bgcolor = "#333333") # Axis area color
p
第一列(一)是文本
所以我的问题是:
- 如何从我的 y 轴(而不是数字)上的任务(第一列)获取文本?
- 如何获得 x 轴上的所有月份?
谢谢。
问题 1 的答案:
您当前的代码没有按照您的意愿执行的原因是:
ticktext = unique(df$one)
由于 df$one
包含 12 个相同的值,因此只有 1 个唯一值,因此不是您需要的 12 个。要解决此问题,您可以只使用 ticktext = df$one
或确保 df$one
中的标签是唯一的(就像您链接到的示例中的情况一样)。例如,将 df$one
更改为 bla1,bla2, ..., bla12 将适用于您当前的示例。
和问题 2:
要指定 x 轴上的刻度间隔,您可以使用 dtick
参数。在您的情况下,这将导致在您的 x 轴代码行中添加以下内容:
xaxis = list(showgrid = F, tickfont = list(color = "#e6e6e6"),
dtick = "M1")
其中 M
指定您希望间隔以月为单位,1 指定您希望间隔为 1 个月(令人震惊!)。仅供参考,这会自动将刻度标签的方向更改为垂直,如果你想调整它,你可以使用 tickangle
参数。
我正在尝试用 R ( the chart I want) 中的 plotly
重现此甘特图。我有一个包含 6 列的数据框,我想在 y 轴上显示文本,在 x 轴上显示月份和年份。根据我的 dataframe
我有以下内容:
one=c('bla','bla','bla',
'bla','bla','bla','bla','bla','bla','bla',
'bla','bla')
two=c('09/25/2017','10/02/2017','11/15/2017','11/29/2017','01/01/2018','01/01/2018','04/01/2018','07/01/2018','09/01/2018','09/01/2018',
'08/01/2020','09/01/2020')
three=c(1102,55,46,214,181,181,122,62,700,700,31,30)
four=c('bla','bla','bla',
'bla','bla','bla','bla',
'bla','bla','bla'
,'bla','bla')
five=c('A','B','C','D','E','F','G','H','E','I','J','E')
df=data.frame(one,two,three,four,five)
df$two =as.Date(df$two,"%m/%d/%Y")
client = "my example"
# Choose colors based on number of resources
cols <- RColorBrewer::brewer.pal(length(unique(df$five)), name = "Set3")
df$color <- factor(df$five, labels = cols)
# Initialize empty plot
p <- plot_ly()
# Each task is a separate trace
# Each trace is essentially a thick line plot
# x-axis ticks are dates and handled automatically
for(i in 1:(nrow(df))){
p <- add_trace(p,
x = c(df$two[i], df$two[i] + df$three[i]), # x0, x1
y = c(i, i), # y0, y1
mode = "lines",
line = list(color = df$color[i], width = 20),
showlegend = F,
hoverinfo = "text",
# Create custom hover text
text = paste("Task: ", df$one[i], "<br>",
"Duration: ", df$three[i], "days<br>",
"Resource: ", df$five[i]),
evaluate = T # needed to avoid lazy loading
)
}
# Add information to plot and make the chart more presentable
p <- layout(p,
# Axis options:
# 1. Remove gridlines
# 2. Customize y-axis tick labels and show task names instead of numbers
xaxis = list(showgrid = F, tickfont = list(color = "#e6e6e6")),
yaxis = list(showgrid = F, tickfont = list(color = "#e6e6e6"),
tickmode = "array", tickvals = 1:nrow(df), ticktext = unique(df$one),
domain = c(0, 0.9)),
# Annotations
annotations = list(
# Add total duration and total resources used
# x and y coordinates are based on a domain of [0,1] and not
# actual x-axis and y-axis values
list(xref = "paper", yref = "paper",
x = 0.80, y = 0.1,
text = paste0("Total Duration: ", sum(df$three), " days<br>",
"Total Resources: ", length(unique(df$five)), "<br>"),
font = list(color = "#ffff66", size = 12),
ax = 0, ay = 0,
align = "left"),
# Add client name and title on top
list(xref = "paper", yref = "paper",
x = 0.1, y = 1, xanchor = "left",
text = paste0("Gantt Chart: ", client),
font = list(color = "#f2f2f2", size = 20, family = "Times New Roman"),
ax = 0, ay = 0,
align = "left")
),
plot_bgcolor = "#333333", # Chart area color
paper_bgcolor = "#333333") # Axis area color
p
第一列(一)是文本
所以我的问题是:
- 如何从我的 y 轴(而不是数字)上的任务(第一列)获取文本?
- 如何获得 x 轴上的所有月份?
谢谢。
问题 1 的答案:
您当前的代码没有按照您的意愿执行的原因是:
ticktext = unique(df$one)
由于 df$one
包含 12 个相同的值,因此只有 1 个唯一值,因此不是您需要的 12 个。要解决此问题,您可以只使用 ticktext = df$one
或确保 df$one
中的标签是唯一的(就像您链接到的示例中的情况一样)。例如,将 df$one
更改为 bla1,bla2, ..., bla12 将适用于您当前的示例。
和问题 2:
要指定 x 轴上的刻度间隔,您可以使用 dtick
参数。在您的情况下,这将导致在您的 x 轴代码行中添加以下内容:
xaxis = list(showgrid = F, tickfont = list(color = "#e6e6e6"),
dtick = "M1")
其中 M
指定您希望间隔以月为单位,1 指定您希望间隔为 1 个月(令人震惊!)。仅供参考,这会自动将刻度标签的方向更改为垂直,如果你想调整它,你可以使用 tickangle
参数。