本文介绍了在R中plotlot和ggplot with facet_grid:如何让yaxis标签使用ticktext值而不是范围值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想积极地使用ggplot2构面,但我遇到了y轴标签显示yaxis范围值而不是ticktext的问题。有什么我可以改变的plotly对象(使用plotly_build)让它正确渲染。我真的很想在ggplots的一个闪亮的应用程序中积极利用悬停功能。我已经发现了一些与情节有关的其他问题,但是大多数问题似乎都是将用户重定向到使用情节剧烈的小情节而不是ggplot。在我的情况下,facet数据并没有消失,yaxis只是被打破了。 参考问题: 同样阴谋破坏yaxis标签: 解决方案这看起来像是从 ggplot 到 Plotly 转换中的一些奇怪的工件。 无论如何,所有你需要做的就是向 ticktext 添加一个空字符串并展开 tickels 1. for(我在2:22){ tick_l< - length(p [['x ']] [['layout']] [[paste('yaxis',i,sep ='')]] [['ticktext']])+ 1 p [['x']] [[ 'seq ='']] [['ticks']] - [seq(1,tick_l)p [['x']] [ ['layout']] [[paste('yaxis',i,sep ='')]] [['ticktext']] [[tick_l]]< - ''} 第一个yaxis布局与其他yaxis布局完全相同,但不需要修复,因为它已经正确显示了。 $ $ $ $ $ $ b $ library $ $ $ $ b $ library $($)$ b $ library $($) #add用于分面的假模型 dt< -data.table(mtcars) dt [,car.id:= rownames(mtcars)] dt [,model:= substr(car.id,1,regexpr(,car.id)-1)] [model ==,model:= car.id] #可选开关:选择几个模型,问题似乎消失#使用数据= dt [%c(Mazda,Merc,Toyota,Honda,Hornet)] ggplot.test< -ggplot(dt,aes(mpg,car.id))+ geom_point()+ facet_grid(model〜。,scales =free_y,space =free,drop = TRUE) p< - ggplotly(ggplot.test) len< - 长度(唯一(dt $模型)) 总值 for(我in 2:len){ total< ; - total + length(p [['x']] [['layout']] [[paste('yaxis',i,sep ='')]] [['ticktext']]) } spacer total_length = total + len * spacer end start tick_l< - length(p [['x']] [['layout']]的b $ b] [[paste('yaxis',i,sep ='')]] [['ticktext']])+ 1 #修正y轴p [['x' ] [['layout']] [[paste('yaxis',i,sep ='')]] [['tickvals']] p [['x ']] [['layout']] [[paste('yaxis',i,sep ='')]] [['ticktext']] [[tick_l]]< - '' end< - start - spacer start< - start - (tick_l - 1)/ total_length v< - c(start,end) #fix size p [['x']] [['layout']] [[paste('yaxis',i,sep ='')]] $ domain< - v } #修复名称与其余部分不同的第一个条目p [['x']] [[ 'layout']] [['annotations']] [[3]] [['y']] p [['x']] [[''布局']] [['shapes']] [[2]] [['y0']] p [['x']] [['layout']] [['annotations']] [[i]] [['y']]< - (p [ 'x']] [['layout']] [[paste('yaxis',i - 2,sep ='')]] $ domain [1] + p [['x']] [['layout' ]] [[paste('yaxis',i - 2,sep ='')]] $ domain [2])/ 2 #trim the text p [['x']] [[ 'layout']] [['annotations']] [[i]] [['text']]< - substr(p [['x']] [['annotations'] ] [[i]] [['text']],1,length(p [['x']] [['layout']] [[paste('yaxis', i - 2,sep ='')]] [['ticktext']])* 3 - 3)} #fix背景中的矩形形状 for (我在seq(0,(len-2)* 2,2)){p [['x']] [['layout']] [['shapes']] [[i + 4] ] [['y0']] p [['x']] [['layout']] [['shapes']] [[i + 4]] [['y1']] } p I would like to use ggplot2 facets with plotly but am running into issues with the y-axis labels showing the yaxis range values instead of the ticktext. Is there something I can alter in the plotly object (using plotly_build) to get it to render correctly. I'd really like to leverage the hover features of plotly in a shiny app on ggplots. I have found a few other questions that are related to facets in plotly but most appear to redirect users to use plotly subplots instead of ggplot. In my case the facet data isn't disappearing, the yaxis is just broken. Reference Question:Error plotting chart with facet_wrap and scales = "free" in plotlySee example code below with mtcars dataset that shows the issues with facets. If you look at the ggplot object the labels are correct.If I reduce the number of subplots like reduce the mtcars data set to only a few models the issue appears to be resolved. Why would # of subplots impact the yaxis tick labels? It also looks like the facets/groups that are broken only have 1 item (unless it is the first yaxis), the facets that have more than 1 item (at least in this example) are plotting the yaxis correctly. Is there something I can alter in the plotly object (using plotly_build) to get it to render correctly. I'd really like to leverage the hover features of plotly in a shiny app on ggplots.library(plotly)library(ggplot2)library(data.table)library(datasets)#add fake model for use in facetdt<-data.table(mtcars)dt[,car.id:=rownames(mtcars)]dt[,model:=substr(car.id,1,regexpr(" ",car.id)-1)][model=="",model:=car.id]#Optional toggle: pick a few models and issue seems to go away #Use data=dt[model %in% c("Mazda","Merc","Toyota","Honda","Hornet")]ggplot.test<-ggplot(dt,aes(mpg,car.id))+geom_point()+facet_grid(model~.,scales="free_y",space="free",drop=TRUE)#check ggplot in Plotsggplot.test#broken ggplotly object in Viewerggplotly(ggplot.test)ggplot Plot:Same plot in plotly that has broken yaxis labels: 解决方案 This looks like some weird artifact from the ggplot to Plotly conversion.Anyways, all what you need to do is to add an empty string to ticktext and expand tickvals by 1. for (i in 2:22) { tick_l <- length(p[['x']][['layout']][[paste('yaxis', i, sep='')]][['ticktext']]) + 1 p[['x']][['layout']][[paste('yaxis', i, sep='')]][['tickvals']] <- seq(1, tick_l) p[['x']][['layout']][[paste('yaxis', i, sep='')]][['ticktext']][[tick_l]] <- ''}The first yaxis layout is identical to the rest but does not need fixing since it already already correctly shown.Fixing the whole plot needs some more tweaking. I tried to make as generic as possible but probably the conversion will break something different for each plot.library(plotly)library(ggplot2)library(data.table)library(datasets) #add fake model for use in facetdt<-data.table(mtcars)dt[,car.id:=rownames(mtcars)]dt[,model:=substr(car.id,1,regexpr(" ",car.id)-1)][model=="",model:=car.id]#Optional toggle: pick a few models and issue seems to go away #Use data=dt[model %in% c("Mazda","Merc","Toyota","Honda","Hornet")]ggplot.test<-ggplot(dt,aes(mpg,car.id))+geom_point()+facet_grid(model~.,scales="free_y",space="free",drop=TRUE)p <- ggplotly(ggplot.test)len <- length(unique(dt$model))total <- 1for (i in 2:len) { total <- total + length(p[['x']][['layout']][[paste('yaxis', i, sep='')]][['ticktext']])}spacer <- 0.01 #space between the horizontal plotstotal_length = total + len * spacerend <- 1start <- 1for (i in c('', seq(2, len))) { tick_l <- length(p[['x']][['layout']][[paste('yaxis', i, sep='')]][['ticktext']]) + 1 #fix the y-axis p[['x']][['layout']][[paste('yaxis', i, sep='')]][['tickvals']] <- seq(1, tick_l) p[['x']][['layout']][[paste('yaxis', i, sep='')]][['ticktext']][[tick_l]] <- '' end <- start - spacer start <- start - (tick_l - 1) / total_length v <- c(start, end) #fix the size p[['x']][['layout']][[paste('yaxis', i, sep='')]]$domain <- v}#fix the first entry which has a different name than the restp[['x']][['layout']][['annotations']][[3]][['y']] <- (p[['x']][['layout']][['yaxis']]$domain[2] + p[['x']][['layout']][['yaxis']]$domain[1]) /2p[['x']][['layout']][['shapes']][[2]][['y0']] <- p[['x']][['layout']][['yaxis']]$domain[1]p[['x']][['layout']][['shapes']][[2]][['y1']] <- p[['x']][['layout']][['yaxis']]$domain[2]#fix the annotationsfor (i in 3:len + 1) { #fix the y position p[['x']][['layout']][['annotations']][[i]][['y']] <- (p[['x']][['layout']][[paste('yaxis', i - 2, sep='')]]$domain[1] + p[['x']][['layout']][[paste('yaxis', i - 2, sep='')]]$domain[2]) /2 #trim the text p[['x']][['layout']][['annotations']][[i]][['text']] <- substr(p[['x']][['layout']][['annotations']][[i]][['text']], 1, length(p[['x']][['layout']][[paste('yaxis', i - 2, sep='')]][['ticktext']]) * 3 - 3)}#fix the rectangle shapes in the backgroundfor (i in seq(0,(len - 2) * 2, 2)) { p[['x']][['layout']][['shapes']][[i+4]][['y0']] <- p[['x']][['layout']][[paste('yaxis', i /2 + 2, sep='')]]$domain[1] p[['x']][['layout']][['shapes']][[i+4]][['y1']] <- p[['x']][['layout']][[paste('yaxis', i /2 + 2, sep='')]]$domain[2]}p 这篇关于在R中plotlot和ggplot with facet_grid:如何让yaxis标签使用ticktext值而不是范围值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-20 22:03