使 R 中的 X 轴标签更细化
Making X-axis labels in R more granular
我正在制作图表和 R,希望 x 轴更细化。
Tenure Type Visits Members Percent
1 0 Basic 122446 283975 0.4311858
2 0 Premium 21190 44392 0.4773383
3 1 Basic 92233 283975 0.3247927
4 1 Premium 17909 44392 0.4034285
5 2 Basic 42516 283975 0.1497174
6 2 Premium 9613 44392 0.2165480
plot(Visit_Curve$Tenure, Visit_Curve$Percent, type = "n", main = "Visit Curve")
lines(Visit_Curve$Tenure[Visit_Curve$Type == "Basic"], Visit_Curve$Percent[Visit_Curve$Type == "Basic"], col = "red")
lines(Visit_Curve$Tenure[Visit_Curve$Type == "Premium"], Visit_Curve$Percent[Visit_Curve$Type == "Premium"], col = "blue")
上面的代码生成了一个图表,其中 x 轴被分解为 50 个因子。有没有一种方法可以轻松地自定义 base R 绘图中的比例?也许每 10 或 5 有一个刻度?
谢谢,
本
只需使用 axes = FALSE
和 ?axis
制作自定义轴
您可以大大清理您的代码:
Visit_Curve <- read.table(header = TRUE, text = "Tenure Type Visits Members Percent
1 0 Basic 122446 283975 0.4311858
2 0 Premium 21190 44392 0.4773383
3 1 Basic 92233 283975 0.3247927
4 1 Premium 17909 44392 0.4034285
5 2 Basic 42516 283975 0.1497174
6 2 Premium 9613 44392 0.2165480")
with(Visit_Curve, {
plot(Tenure, Percent, type = 'n', main = 'Visit Curve', axes = FALSE)
axis(1, at = seq(0, 2, by = .15))
axis(2, at = seq(.15, .45, by = .05), las = 1)
box('plot')
lines(Tenure[Type == 'Basic'], Percent[Type == 'Basic'], col = 'red')
lines(Tenure[Type == 'Premium'], Percent[Type == 'Premium'], col = 'blue')
})
或使用 xaxt = 'n'
或 yaxt = 'n'
单独抑制每个轴上的标签
with(Visit_Curve, {
plot(Tenure, Percent, type = 'n', main = 'Visit Curve', xaxt = 'n', las = 1)
axis(1, at = seq(0, 2, by = .15))
lines(Tenure[Type == 'Basic'], Percent[Type == 'Basic'], col = 'red')
lines(Tenure[Type == 'Premium'], Percent[Type == 'Premium'], col = 'blue')
})
或使用ggplot2
library('ggplot2')
ggplot(Visit_Curve, aes(Tenure, Percent, colour = Type)) +
geom_line() +
scale_x_continuous(breaks = seq(0, 2, .15)) +
labs(title = 'Visit Curve')
我正在制作图表和 R,希望 x 轴更细化。
Tenure Type Visits Members Percent
1 0 Basic 122446 283975 0.4311858
2 0 Premium 21190 44392 0.4773383
3 1 Basic 92233 283975 0.3247927
4 1 Premium 17909 44392 0.4034285
5 2 Basic 42516 283975 0.1497174
6 2 Premium 9613 44392 0.2165480
plot(Visit_Curve$Tenure, Visit_Curve$Percent, type = "n", main = "Visit Curve")
lines(Visit_Curve$Tenure[Visit_Curve$Type == "Basic"], Visit_Curve$Percent[Visit_Curve$Type == "Basic"], col = "red")
lines(Visit_Curve$Tenure[Visit_Curve$Type == "Premium"], Visit_Curve$Percent[Visit_Curve$Type == "Premium"], col = "blue")
上面的代码生成了一个图表,其中 x 轴被分解为 50 个因子。有没有一种方法可以轻松地自定义 base R 绘图中的比例?也许每 10 或 5 有一个刻度?
谢谢, 本
只需使用 axes = FALSE
和 ?axis
您可以大大清理您的代码:
Visit_Curve <- read.table(header = TRUE, text = "Tenure Type Visits Members Percent
1 0 Basic 122446 283975 0.4311858
2 0 Premium 21190 44392 0.4773383
3 1 Basic 92233 283975 0.3247927
4 1 Premium 17909 44392 0.4034285
5 2 Basic 42516 283975 0.1497174
6 2 Premium 9613 44392 0.2165480")
with(Visit_Curve, {
plot(Tenure, Percent, type = 'n', main = 'Visit Curve', axes = FALSE)
axis(1, at = seq(0, 2, by = .15))
axis(2, at = seq(.15, .45, by = .05), las = 1)
box('plot')
lines(Tenure[Type == 'Basic'], Percent[Type == 'Basic'], col = 'red')
lines(Tenure[Type == 'Premium'], Percent[Type == 'Premium'], col = 'blue')
})
或使用 xaxt = 'n'
或 yaxt = 'n'
with(Visit_Curve, {
plot(Tenure, Percent, type = 'n', main = 'Visit Curve', xaxt = 'n', las = 1)
axis(1, at = seq(0, 2, by = .15))
lines(Tenure[Type == 'Basic'], Percent[Type == 'Basic'], col = 'red')
lines(Tenure[Type == 'Premium'], Percent[Type == 'Premium'], col = 'blue')
})
或使用ggplot2
library('ggplot2')
ggplot(Visit_Curve, aes(Tenure, Percent, colour = Type)) +
geom_line() +
scale_x_continuous(breaks = seq(0, 2, .15)) +
labs(title = 'Visit Curve')