Plotly。如何将ggplot对象转换成Plotly?

问题描述 投票:1回答:2

我有一个数据集,有两列.第一列的名字是Centile.threshold,第二列是Effective.tax.rates。所以接下来陡是用下面这段代码做图。

# Data and code
library("rJava")
library("xlsxjars")
library("xlsx")
require(tidyr)
require(plyr)
library("ggplot2")
library("plotly")

    g4_data_ext<-data.frame(structure(list(Centile.threshold = c(1, 2, 3, 4, 5, 6, 7, 8, 
                                     9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 
                                     25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 
                                     41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 
                                     57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 
                                     73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 
                                     89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100), Effective.tax.rates = c(11.4875111183361, 
                                                                                                               8.07673874931679, 7.8639563682086, 6.84656368538612, 6.8383437884744, 
                                                                                                               5.83532681932012, 5.11955857753708, 4.68757220539562, 4.66492423437793, 
                                                                                                               4.63051650494602, 4.71570390296145, 4.4419481131279, 4.16374366969064, 
                                                                                                               4.4424750798236, 4.2633646392858, 4.10185821346884, 2.29819561722, 
                                                                                                               2.01895390855722, 0, 0, 2.80530318111453, 2.83367683951859, 3.03173307975026, 
                                                                                                               3.58758933227946, 3.05869003045607, 3.59225918463074, 3.01588015121759, 
                                                                                                               3.55943967998446, 3.6220565232692, 3.40885422585891, 3.51447401518606, 
                                                                                                               3.68902868712004, 3.77018314638409, 3.72783452684771, 3.80791342516448, 
                                                                                                               3.99449874248864, 4.08421307782513, 4.07517557211112, 4.19659018929637, 
                                                                                                               4.22887420998102, 4.25529382081159, 4.36205679154288, 4.40690982734329, 
                                                                                                               4.33909305037396, 4.45990415426276, 4.59436808108174, 4.6831546716255, 
                                                                                                               4.73811656768519, 4.75412915916737, 4.84778797281815, 4.94690785473091, 
                                                                                                               5.06784298188807, 5.1769208879221, 5.2520552039406, 5.33650672817794, 
                                                                                                               5.43499638671921, 5.52400199193912, 5.58169115527766, 5.70509046165446, 
                                                                                                               5.76549758450655, 5.86333222670147, 5.87097687497217, 5.98729128544292, 
                                                                                                               6.07397530734785, 6.15030076581313, 6.21615540600908, 6.28135059352123, 
                                                                                                               6.38292345287997, 6.43416757218245, 6.5863284138631, 6.63365437304645, 
                                                                                                               6.70316768627345, 6.7816891944299, 6.85128738244695, 6.94261253911407, 
                                                                                                               7.01673024329712, 7.11081973369591, 7.18077796481166, 7.26197149513331, 
                                                                                                               7.32607460317916, 7.39638728837014, 7.47062968448649, 7.55194205005014, 
                                                                                                               7.64318101794584, 7.73728594723894, 7.79092205170689, 7.88152530983832, 
                                                                                                               7.97428540786095, 8.09278589483141, 8.20373396784042, 8.27757060469128, 
                                                                                                               8.40889176349213, 8.50851684368756, 8.64124701008068, 8.72559960562268, 
                                                                                                               8.85276486059087, 9.06564270204267, 9.26861906650096, 9.43047799204161, 
                                                                                                               10.2298639144453), grp_id = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                                                                                                                                             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                                                                                                                                             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 
                                                                                                                                             2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 
                                                                                                                                             4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 
                                                                                                                                             6, 6, 6, 7, 8), grp_label = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                                                                     1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                                                                     1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                                                                     1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 
                                                                                                                                                                                     4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
                                                                                                                                                                                     6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 
                                                                                                                                                                                     7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 2L, 3L), .Label = c("<=50%", 
                                                                                                                                                                                                                                                         "=99%", ">99%", "51%-60%", "61%-70%", "71%-80%", "81%-90%", "91%-98%"
                                                                                                                                                                                     ), class = "factor")), row.names = c(NA, -100L), class = "data.frame"))

这是用ggplot做图的代码。这个代码很好用。

    g4_data_ext<-data.frame(g4_data, grp_id=NA, grp_label=NA)

for (i in 1:length((g4_data$Centile.threshold)))
{
  if (g4_data$Centile.threshold[i]<=50)
  {
    g4_data_ext$grp_label[i] <- "<=50%"
    g4_data_ext$grp_id[i] <- 1
  }
  else if (51<=g4_data$Centile.threshold[i] & g4_data$Centile.threshold[i]<=60) 
  {
    g4_data_ext$grp_label[i] <- "51%-60%" 
    g4_data_ext$grp_id[i] <- 2
  }
  else if (61<=g4_data$Centile.threshold[i] & g4_data$Centile.threshold[i]<=70) 
  {
    g4_data_ext$grp_label[i] <- "61%-70%"  
    g4_data_ext$grp_id[i] <- 3
  }
  else if (71<=g4_data$Centile.threshold[i] & g4_data$Centile.threshold[i]<=80) 
  {
    g4_data_ext$grp_label[i] <- "71%-80%"  
    g4_data_ext$grp_id[i] <- 4
  }
  else if (81<=g4_data$Centile.threshold[i] & g4_data$Centile.threshold[i]<=90) 
  {
    g4_data_ext$grp_label[i] <- "81%-90%" 
    g4_data_ext$grp_id[i] <- 5
  }
  else if (90<=g4_data$Centile.threshold[i] & g4_data$Centile.threshold[i]<=98) 
  {
    g4_data_ext$grp_label[i] <- "91%-98%"  
    g4_data_ext$grp_id[i] <- 6
  }
  else if (g4_data$Centile.threshold[i]==99) 
  {
    g4_data_ext$grp_label[i] <- "=99%"
    g4_data_ext$grp_id[i] <- 7
  }
  else 
  {
    g4_data_ext$grp_label[i] <- ">99%"
    g4_data_ext$grp_id[i] <- 8
  }
}

g4_data_ext$grp_label<-factor(g4_data_ext$grp_label)

color_code<-factor(c(rep("dark turquoise", 6), "tomato", "orange red"))


means_g4<-data.frame(ddply(g4_data_ext,~grp_id+grp_label,summarise,mean=mean(Effective.tax.rates),sd=sd(Effective.tax.rates)), color_code)

ggplot(data = means_g4, aes(x=factor(grp_id),y=mean, label =mean ))+
  scale_x_discrete("Group",breaks=c(1:8), labels=means_g4$grp_label)+
  coord_cartesian(ylim = c(-3, 12)) +
  geom_bar(stat="identity", fill=color_code)+
  geom_text(aes(y = mean*1.1, label = round(mean,2)), position=position_dodge(0.9))+
  theme(axis.text.x = element_text(angle = 0))

但我的目的是用ggplotly()转换这个情节.我试着转换,但工作得很好,所以有人能帮助我如何将这个情节转换为plotly对象吗?

r ggplot2 plotly ggplotly
2个回答
3
投票

当我尝试

ggplotly(ggplot(data = means_g4, aes(x=factor(grp_id),y=mean, label =mean ))+
  scale_x_discrete("Group",breaks=c(1:8), labels=means_g4$grp_label)+
  coord_cartesian(ylim = c(-3, 12)) +
  geom_bar(stat="identity", fill=color_code)+
  geom_text(aes(y = mean*1.1, label = round(mean,2)), position=position_dodge(0.9))+
  theme(axis.text.x = element_text(angle = 0)))

我得到的错误是

Error in nchar(axisObj$ticktext) : 'nchar()' requires a character vector

如果我不使用 scale_x_discrete 术语。 唯一看起来应该是一个字符向量的东西是 labels=means_g4$grp_label事实上 means_g4$grp_label 是一个因素。 所以,将其转换,并将其 ggplotly() 工作。

ggplotly(ggplot(data = means_g4, aes(x=factor(grp_id),y=mean, label =mean ))+
  scale_x_discrete("Group",breaks=c(1:8), labels=as.character(means_g4$grp_label))+
  coord_cartesian(ylim = c(-3, 12)) +
  geom_bar(stat="identity", fill=color_code)+
  geom_text(aes(y = mean*1.1, label = round(mean,2)), position=position_dodge(0.9))+
  theme(axis.text.x = element_text(angle = 0)))

这看起来像是一个错误 ggplotly(),应该是自动进行转换的。


2
投票

user2554330 已经解决了你问题中的难点。剩下的就很简单了,因为我没有看到你将ggplot转换为plotly的实际尝试,所以你可以这样做。

从 "标准 "的ggplot方法来看,只需在其中加入 ggplotly:

p <- ggplot(...)
p <- p + geom_point(...)  # just an example...

fig <- ggplotly(p)
fig

这里有一个例子。

library(plotly)
library(tidyverse)
library(ggplot2)


# ggplot
p <- ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width)) 
p <- p + geom_point(aes(color=Species, shape=Species)) +
                    xlab("Sepal Length") +  ylab("Sepal Width") +
                    ggtitle("Sepal Length-Width")
# plotly
fig <- ggplotly(p)
fig

剧情:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.