为什么我不能标记 X 轴?
Why can't I get the X axis to be labeled?
出于某种原因,我无法设法显示 x 轴标签。上下文:这应该是一周内的连续工作。
library(ggplot2)
library(dplyr)
# FIGURE #####
oneseventh <- 1/7
work <-c(oneseventh)
work <- rep(work,each=7)
days_num <-c("1","2","3","4","5","6","7")
days_vec <- c(1:7)
npro_data <- data.frame(work)
npro_plot <- ggplot(npro_data, aes(x=1:7, y=work,))
npro_plot + labs(x = "Days of the Week",
y ="Amount of Work per day (%)",
title = "Non-Procrastinator",
tag = "A",
caption = "(based on data from ...)") +
coord_cartesian(xlim =c(1, 7), ylim = c(0,100)) +
scale_x_discrete(labels=days_num,breaks=days_num) +
geom_point() +
geom_line()
#
查看我用这段代码得到的图:
由于您在 ggplot(aes)
中使用了 x = 1:7
,因此您必须指定一个 scale_x_continuous
(因为 1:7
将是一个连续变量,而不是分类变量或离散变量)。另外,你不需要在scale_x_continuous
中使用labels
参数,设置breaks
就足够了。
如果可能,我建议在输入数据帧中包含 ggplot
中涉及的所有变量(即您的 npro_data
)。它会更容易处理和避免歧义。
library(ggplot2)
ggplot(npro_data, aes(x=1:7, y=work,)) +
labs(x = "Days of the Week",
y ="Amount of Work per day (%)",
title = "Non-Procrastinator",
tag = "A",
caption = "(based on data from ...)") +
coord_cartesian(xlim =c(1, 7), ylim = c(0,100)) +
scale_x_continuous(breaks = days_vec) +
geom_point() +
geom_line()
但是,由于在您的问题中提到您希望 x-axis 是工作日,因此最好在 npro_data
中包含 days_num
,并且将其映射到 ggplot
中的 x 美学。如果是这种情况,您需要使用 scale_x_discrete
来更改标签和中断。
出于某种原因,我无法设法显示 x 轴标签。上下文:这应该是一周内的连续工作。
library(ggplot2)
library(dplyr)
# FIGURE #####
oneseventh <- 1/7
work <-c(oneseventh)
work <- rep(work,each=7)
days_num <-c("1","2","3","4","5","6","7")
days_vec <- c(1:7)
npro_data <- data.frame(work)
npro_plot <- ggplot(npro_data, aes(x=1:7, y=work,))
npro_plot + labs(x = "Days of the Week",
y ="Amount of Work per day (%)",
title = "Non-Procrastinator",
tag = "A",
caption = "(based on data from ...)") +
coord_cartesian(xlim =c(1, 7), ylim = c(0,100)) +
scale_x_discrete(labels=days_num,breaks=days_num) +
geom_point() +
geom_line()
#
查看我用这段代码得到的图:
由于您在 ggplot(aes)
中使用了 x = 1:7
,因此您必须指定一个 scale_x_continuous
(因为 1:7
将是一个连续变量,而不是分类变量或离散变量)。另外,你不需要在scale_x_continuous
中使用labels
参数,设置breaks
就足够了。
如果可能,我建议在输入数据帧中包含 ggplot
中涉及的所有变量(即您的 npro_data
)。它会更容易处理和避免歧义。
library(ggplot2)
ggplot(npro_data, aes(x=1:7, y=work,)) +
labs(x = "Days of the Week",
y ="Amount of Work per day (%)",
title = "Non-Procrastinator",
tag = "A",
caption = "(based on data from ...)") +
coord_cartesian(xlim =c(1, 7), ylim = c(0,100)) +
scale_x_continuous(breaks = days_vec) +
geom_point() +
geom_line()
但是,由于在您的问题中提到您希望 x-axis 是工作日,因此最好在 npro_data
中包含 days_num
,并且将其映射到 ggplot
中的 x 美学。如果是这种情况,您需要使用 scale_x_discrete
来更改标签和中断。