Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将ggplot中的y限值与刻面_包裹更改为对数刻度和常规刻度的混合_R_Facet Wrap - Fatal编程技术网

R 将ggplot中的y限值与刻面_包裹更改为对数刻度和常规刻度的混合

R 将ggplot中的y限值与刻面_包裹更改为对数刻度和常规刻度的混合,r,facet-wrap,R,Facet Wrap,我有一个数据集,其中一个组的值范围很广。使用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,

我有一个数据集,其中一个组的值范围很广。使用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))
set.seed(123)

FiveLetters您可以进行转换后的每月计数,并将其用作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))
##修改月度盘点
df$mcount这就可以了

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))

到目前为止,最简单的方法是绘制两个图,并用
网格将它们粘在一起。排列
而不是刻面。“log_trans()”做什么?使用最新的ggplot2运行此函数时,该函数未知。是否尝试指定范围scales::log_trans()