2017-10-05 2 views
1

Je me demandais s'il y a un moyen d'avoir une icône personnalisée pour diagramme circulaire de plotly au lieu de la division habituelle tarteUtilisez une icône personnalisée dans le graphique circulaire de plotly

A partir de maintenant j'afficher les informations de genre en utilisant une tarte tableau qui se présente comme ci-dessous:

enter image description here

Je suis en train de la faire ressembler à l'intrigue de genre dans le lien ci-dessous:

https://app.displayr.com/Dashboard?id=c1506180-fe64-4941-8d24-9ec4a54439af#page=3e133117-f3b2-488b-bc02-1c2619cf3914

enter image description here

Le code plotly est comme sous:

plot_ly(genderselection, labels = ~Gender, values = ~Freq, type = 'pie') %>% 
     layout(title = paste0("Gender Distribution of Patients from Boston"), 
      xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE), 
      yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE), 
      legend=list(orientation='h')) 

Le genderselection dataframe:

Gender Freq 
    F 70 
    M 65 

Si vous n'utilisez est plotly-il une autre bibliothèque qui peut être utilisé pour afficher des informations en utilisant icônes personnalisées?

Répondre

2

(1) Téléchargez le fichier .png disponible here et enregistrez-le dans votre répertoire de travail comme man_woman.png
(2) Exécutez le code suivant:

library(png) 
library(plotly) 

genderselection <- read.table(text=" 
    Gender Freq 
    F 70 
    M 30 
", header=T) 
pcts <- round(prop.table(genderselection$Freq)*100) 

# Load png file with man and woman 
img <- readPNG("man_woman.png") 
h <- dim(img)[1] 
w <- dim(img)[2] 

# Find the rows where feet starts and head ends 
pos1 <- which(apply(img[,,1], 1, function(y) any(y==1))) 
mn1 <- min(pos1) 
mx1 <- max(pos1) 
pospctM <- round((mx1-mn1)*pcts[2]/100+mn1) 
pospctF <- round((mx1-mn1)*pcts[1]/100+mn1) 

# Fill bodies with a different color according to percentages 
imgmtx <- img[h:1,,1] 
whitemtx <- (imgmtx==1) 
colmtx <- matrix(rep(FALSE,h*w),nrow=h) 
midpt <- round(w/2)-10 
colmtx[mx1:pospctM,1:midpt] <- TRUE 
colmtx[mx1:pospctF,(midpt+1):w] <- TRUE 
imgmtx[whitemtx & colmtx] <- 0.5 

# Plot matrix using heatmap and print text 
labs <- c(paste0(pcts[2], "% Males"),paste0(pcts[1], "% Females")) 
ax <- list(ticks='', showticklabels=FALSE, showgrid=FALSE, zeroline=FALSE) 
p <- plot_ly(z = imgmtx, showscale=FALSE, type='heatmap', width = 500, height = 500) %>% 
    add_text(x = c(100,250), y = c(20,20), type='heatmap', mode="text", 
     text=labs, showlegend=FALSE, textfont=list(size=20, color="#FFFFFF"), inherit=FALSE) %>% 
    layout(xaxis = ax, yaxis = ax) 
p 

enter image description here

+0

C'est exactement ce que je recherchais pour ! Merci! Y a-t-il un moyen de changer le fond en blanc et le remplissage de couleur? Je vois que vous avez rempli les corps avec des couleurs différentes en fonction des pourcentages, mais prend-il les couleurs remplies par défaut? Pouvons-nous définir les couleurs selon notre choix? –

+1

@DollarVora Ici vous pouvez trouver un moyen de contrôler la palette de couleurs pour le 'heatmap' de l'intrigue: https://stackoverflow.com/questions/44918709/how-to-generate-a-custom-color-scale-for-plotly- carte de chaleur –