[英]ggplot output with multiple scales
我有一个不同类型(“数字”或“货币”)的数据需要融合并在图表中输出。 我不知道如何适应多个尺度。 以下是该问题的一个示例:
library(scales); library(melt)
# Generate some random
V1 <- 1:20
V2 <- sample(1:100, 10, replace = F)
V3 <- sample(1: 100, 10, replace = F)
V3 <- dollar(V3)
# Melt data
data <- data.frame(V1 = V1, V2 = V2, V3 = V3)
data_melt <- melt(data, id.vars = c("V1"))
# Now I want to plot this data with different scales
ggplot(data_melt, aes(x = V1, y = value , col = variable)) +
geom_point()
scales::dollar
将V3
转换为字符串,然后data.frame
将其转换为因子,然后reshape2::melt
将与V2
合并,将V2
从整数和V3
从因子转换为字符串。 这些都不是你想要的。
相反,将您的数据保留为数字,直到在 ggplot 内部,您可以在其中将适当的scales
函数传递给scale_y_continuous
,例如
library(tidyverse)
set.seed(47)
df_wide <- data_frame(V1 = 1:20,
V2 = sample(100, 20, replace = TRUE),
V3 = sample(100, 20, replace = TRUE))
df_long <- df_wide %>% gather(variable, value, -V1)
ggplot(df_long, aes(V1, value, color = variable)) +
geom_point() +
scale_y_continuous(label = scales::dollar_format())
但是,您不能同时格式化和不格式化您的轴,因此如果V1
和V2
具有不同的单位,您必须做出选择。 另一种选择是分别对它们进行分面或绘制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.