我有一个数据集,其中包含一组广泛的值。使用ggplot的facet_wrap,我会在一个组(具有最宽值范围的组)和另一组的常规轴的对数刻度中绘制y轴。
以下是可重现的例子。
set.seed(123)
FiveLetters <- LETTERS[1:2]
df <- data.frame(MonthlyCount = sample(1:10, 36, replace=TRUE),
CustName = factor(sample(FiveLetters,size=36, replace=TRUE)),
ServiceDate = format(seq(ISOdate(2003,1,1), by='day', length=36),
format='%Y-%m-%d'), stringsAsFactors = F)
df$ServiceDate <- as.Date(df$ServiceDate)
# replace some counts to really high numbers for group A
df$MonthlyCount[df$CustName =="A" & df$MonthlyCount >= 9 ] <-300
df
library(ggplot2)
library(scales)
ggplot(data = df, aes(x = ServiceDate, y = MonthlyCount)) +
geom_point() +
facet_wrap(~ CustName, ncol = 1, scales = "free_y" ) +
scale_x_date("Date",
labels = date_format("%Y-%m-%d"),
breaks = date_breaks("1 week")) +
theme(axis.text.x = element_text(colour = "black",
size = 16,
angle = 90,
vjust = .5))
结果图有两个方面。 A组的方面在图表的顶部和底部有点,难以比较,B的方面更容易阅读。我想在对数刻度中为A组绘制facet,并让其他人“自由”。
答案 0 :(得分:1)
这是工作
ggplot(data = df, aes(x = ServiceDate, y = MonthlyCount)) +
geom_point() +
facet_wrap(~ CustName, ncol = 1, scales = "free_y" ) +
scale_x_date("Date",
labels = date_format("%Y-%m-%d"),
breaks = date_breaks("1 week")) +
scale_y_continuous(trans=log_trans(), breaks=c(1,3,10,50,100,300),
labels = comma_format())+
theme(axis.text.x = element_text(colour = "black",
size = 16,
angle = 90,
vjust = .5))
答案 1 :(得分:0)
您可以进行转换的每月计数并将其用作y轴。
## modify monthly count
df$mcount <- with(df, ifelse(CustName == "A", log(MonthlyCount), MonthlyCount))
ggplot(data = df, aes(x = ServiceDate, y = mcount)) +
geom_point() +
facet_wrap(~ CustName, ncol = 1, scales = "free_y" ) +
scale_x_date("Date",
labels = date_format("%Y-%m-%d"),
breaks = date_breaks("1 week")) +
theme(axis.text.x = element_text(colour = "black",
size = 16,
angle = 90,
vjust = .5))