2017-10-05 13 views
1

궁금 해서요을 사용자 정의 아이콘을 사용하여 아래 보이는 차트 :는 plotly의 파이 차트에서

enter image description here

나는 그것이 아래 링크에서 성별 플롯처럼 보이게하기 위해 노력하고있어 :

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

,691 363,210

enter image description here

plotly 코드는 아래 등 :

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

genderselection의 dataframe :

Gender Freq 
    F 70 
    M 65 

plotly 사용하지 않을 경우 사용 정보를 표시하는 데 사용될 수있는 임의의 다른 라이브러리가 사용자 정의 아이콘?

답변

2

(1) 사용할 수있는 PNG 파일 here을 다운로드로 작업 디렉토리에 저장 man_woman.png
(2) 다음 코드를 실행

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

이 정확히 내가 찾던를 ~! 감사합니다. 배경을 흰색으로 변경하고 색상을 채우는 방법이 있습니까? 나는 당신이 백분율에 따라 다른 색깔을 가진 몸을 채웠다는 것을 보았습니다 그러나 채워진 색깔을 기본적으로 가지고 가는가? 우리가 선택한대로 색상을 정의 할 수 있습니까? –

+1

@DollarVora 여기에 플롯 히트 맵에 대한 컬러 팔레트를 제어하는 ​​방법이 있습니다 : https://stackoverflow.com/questions/44918709/how-to-generate-a-custom-color-scale-for-plotly- 히트 맵 –