R:向图形添加比例
R: Adding a scale to graph
我已经在 R 中创建了我想要的图表,但它没有按我预期的方式缩放。我创建的图表如下。 x 轴是年龄,目前列为“10、11、12、13...91 岁及以上”。我希望它更像是“10、20、30...91 及以上”。所以 x 轴是可读的。我将如何在 R 中做到这一点?该图的代码也在下面。
ggplot(data, aes(Age,Shoes))+
geom_density(stat="identity")+
facet_wrap(~Gender)
数据是我的数据集,包括年龄、鞋子、性别三列。
任何帮助是极大的赞赏!
感谢大家!
莉齐
你想在你的案例中使用 scale_x_continuous()
,正如 alistaire 所建议的。您将在函数中指定要中断的位置。在下面的示例中,我指定了 5 和 10。
# Create a sample data
mydf <- data.frame(age = rep(c(1, 2, 4, 10, 6, 7, 5, 5, 8, 10), times = 10),
shoes = runif(n = 100, min = 20, max = 75))
### X is specified
ggplot(data = mydf, aes(x = age, y = shoes)) +
geom_bar(stat = "identity") +
scale_x_continuous(breaks = c(5, 10))
### Default
ggplot(data = mydf, aes(x = age, y = shoes)) +
geom_bar(stat = "identity")
我已经在 R 中创建了我想要的图表,但它没有按我预期的方式缩放。我创建的图表如下。 x 轴是年龄,目前列为“10、11、12、13...91 岁及以上”。我希望它更像是“10、20、30...91 及以上”。所以 x 轴是可读的。我将如何在 R 中做到这一点?该图的代码也在下面。
ggplot(data, aes(Age,Shoes))+
geom_density(stat="identity")+
facet_wrap(~Gender)
数据是我的数据集,包括年龄、鞋子、性别三列。 任何帮助是极大的赞赏! 感谢大家! 莉齐
你想在你的案例中使用 scale_x_continuous()
,正如 alistaire 所建议的。您将在函数中指定要中断的位置。在下面的示例中,我指定了 5 和 10。
# Create a sample data
mydf <- data.frame(age = rep(c(1, 2, 4, 10, 6, 7, 5, 5, 8, 10), times = 10),
shoes = runif(n = 100, min = 20, max = 75))
### X is specified
ggplot(data = mydf, aes(x = age, y = shoes)) +
geom_bar(stat = "identity") +
scale_x_continuous(breaks = c(5, 10))
### Default
ggplot(data = mydf, aes(x = age, y = shoes)) +
geom_bar(stat = "identity")