이 질문은 여러 번 묻는 것이지만 음모에 대한 기본 구문 중 일부는 변경된 것으로 생각됩니다. ggplotly()
을 사용하여 choropleth 맵을 작성하면 내 미학에서 길고, 어둡고, 그룹이며, 내 변수 중 하나의 기본 툴팁이 제공됩니다. 나는 툴팁이 미학에서의 것만을 매핑한다는 것을 이해한다. 내가하고 싶은 일은 툴팁을 사용자 정의하여 내 데이터 세트 (미학에 매핑되지 않은 변수 포함)에 다른 변수 (좌표 등)가 아닌 일부 변수를 표시하는 것입니다. 아래는 재현 할 수있는 예제이며 지금까지 해보았습니다. 나는 아무런 도움이되지 않는 다른 질문에 대한 답변으로 주어진 조언을 따랐다.rg3에서 ggplot2를 사용하여 플롯 맵에 대한 툴팁의 레이블 편집
#Load dependencies
library(rgeos)
library(stringr)
library(rgdal)
library(maptools)
library(ggplot2)
library(plotly)
#Function to read shapefile from website
dlshape=function(shploc, shpfile) {
temp=tempfile()
download.file(shploc, temp)
unzip(temp)
shp.data <- sapply(".", function(f) {
fp <- file.path(temp, f)
return(readOGR(".",shpfile))
})
}
austria <- dlshape(shploc="http://biogeo.ucdavis.edu/data/gadm2.8/shp/AUT_adm_shp.zip",
"AUT_adm1")[[1]]
#Create random data to add as variables
[email protected]$example1<-sample(seq(from = 1, to = 100, by = 1), size = 11, replace = TRUE)
[email protected]$example2<-sample(seq(from = 1, to = 100, by = 1), size = 11, replace = TRUE)
[email protected]$example3<-sample(seq(from = 1, to = 100, by = 1), size = 11, replace = TRUE)
#Fortify shapefile to use w/ ggplot
austria.ft <- fortify(austria, region="ID_1")
data<-merge(austria.ft, austria, region="id", by.x = "id", by.y = "ID_1")
#Save as ggplot object
gg<-ggplot(data, aes(x = long, y = lat, fill = example1, group = group)) +
geom_polygon() + geom_path(color="black",linetype=1) +
coord_equal() +
scale_fill_gradient(low = "lightgrey", high = "darkred", name='Index') +xlab("")+ylab("") +
theme(axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank()) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black")) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))
#Plot using ggplotly
ggplotly(gg)
여기에서 두 가지 접근 방식을 시도했습니다. 가장 성공적인 방법 중 하나가 나를 거기에 데려옵니다. 새 변수를 툴팁에 추가 할 수는 있지만 두 가지를 할 수는 없습니다. 1) 이미 기본적으로 (미학에서) 표시된 다른 변수를 제거 할 수 없으며 2) 변수 이름을 해당 열 이름 이외의 다른 이름으로 바꿀 수는 없습니다. . 데이터 세트 (예를 들어 내가 예 III ""로 예 3을 "레이블 싶습니다) 여기에 그 접근 방식 : 나는 또한 다음과 같은 추가하는 시도
#Save as a new ggplot object except this time add ``label = example3`` to the aesthetics
gg2<-ggplot(data, aes(x = long, y = lat, fill = example1, group = group, label = example3)) +
geom_polygon() + geom_path(color="black",linetype=1) +
coord_equal() +
scale_fill_gradient(low = "lightgrey", high = "darkred", name='Index') +xlab("")+ylab("") +
theme(axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank()) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black")) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))
#Save as plotly object then plot
gg2 <- plotly_build(gg2)
gg2
하지만 아무것도하지 않았다 :
gg2$data[[1]]$text <- paste("Example I:", data$example1, "<br>",
"Example II:", data$example2, "<br>",
"Example III:", data$example3)
어떤 도움
업데이트 : 0 업데이트 됨 CRAN 대신 github에서 설치하여을 설치하십시오. 이 업데이트 된 버전 (4.0.0)을 사용하여 저는 거기에 그 방법을 제공했습니다.
gg2$x$data[[2]]$text <- paste("Example I:", data$example1, "<br>",
"Example II:", data$example2, "<br>",
"Example III:", data$example3)
gg2
지금은 어떻게됩니까? 그러면 이전 툴팁과는 다른 툴팁이 추가됩니다. 이 새로운 툴팁은 정확히 내가 원하는 것일뿐입니다. 그러나 둘 다 나타납니다. 그러나 마우스를 움직이면 즉시 나타납니다. 아래의 두 스크린 샷을 참조하십시오
공지 사항 그 툴팁 동일한 단위 (티롤)에서 있습니다. 이 패키지의 버그 일 수 있습니까? 지도가 아닌 시계와 같은 다른 그래프를 표시 할 때 발생하지 않습니다. 또한 "Example I"(또는 II 또는 III) 레이블을 지정했는데 추가 한 새 툴팁에는 표시되지 않습니다.
업데이트 2 : 이전 도구 설명 (길고 위도가 표시된)이 국경 위로 마우스를 가져 가면 나타납니다. 따라서 국경을 제거하기 위해 geom_path(color="black",linetype=1)
명령을 제거하고 관리했습니다. 그 문제를 성공적으로 해결하십시오. 그러나 여전히 툴팁에 표시되는 라벨을 수정할 수 없습니다.
업데이트 # 3 : 레이블을 편집하는 방법을 알아 냈지만 오직 한 가지만 변경할 수 있습니다. 어느 견과류입니다! 다음 그래프를 생성
#Load dependencies
library(rgeos)
library(stringr)
library(rgdal)
library(maptools)
library(ggplot2)
library(plotly)
#Function to read shapefile from website
dlshape=function(shploc, shpfile) {
temp=tempfile()
download.file(shploc, temp)
unzip(temp)
shp.data <- sapply(".", function(f) {
fp <- file.path(temp, f)
return(readOGR(".",shpfile))
})
}
austria <- dlshape(shploc="http://biogeo.ucdavis.edu/data/gadm2.8/shp/AUT_adm_shp.zip",
"AUT_adm1")[[1]]
#Create random data to add as variables
[email protected]$example1<-sample(seq(from = 1, to = 100, by = 1), size = 11, replace = TRUE)
[email protected]$example2<-sample(seq(from = 1, to = 100, by = 1), size = 11, replace = TRUE)
[email protected]$example3<-sample(seq(from = 1, to = 100, by = 1), size = 11, replace = TRUE)
#Fortify shapefile to use w/ ggplot
austria.ft <- fortify(austria, region="ID_1")
data<-merge(austria.ft, austria, region="id", by.x = "id", by.y = "ID_1")
#Save as ggplot object
gg<-ggplot(data, aes(x = long, y = lat, fill = example1, group = group, text = paste("Province:", NAME_1))) +
geom_polygon(color="black", size=0.2) +
coord_equal() +
scale_fill_gradient(low = "lightgrey", high = "darkred", name='Index') +xlab("")+ylab("") +
theme(axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank()) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black")) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))
gg <- plotly_build(gg)
gg
: "주"는 (그렇지 예전) 지금 대문자입니다
공지 사항 여기에 처음부터 끝까지 내 워크 플로우입니다. 트릭은 미학에 text = paste("Province:", NAME_1)
을 추가하는 것이 었습니다.내가 text2=paste("Example III:", example1)
사용하여 추가 라벨 변경을 추가하려고 할 때 그러나 다음과 같은 상황이 발생합니다 그것이 텍스트 1 렌더링 같은 방법으로 텍스트 2 렌더링 할 수
공지있다.
나는 "전설"옵션에 토글 같은 간단한 생각하기 시작 해요 : 다음과 같은 이상한 결과를 생성 - 어떤 text=paste("Example III:", example1)
: 그래서 그 대신 나는 단순히 다음과 같은 텍스트 2없이 중복을 추가하려고 음모의 ggplot 변환은 불가능합니다. 업데이트 # 4 : 그래서 나는 다른 방식으로 접근하기로 결정했습니다. 대신 변수 이름 자체를 변경하기로 결정했습니다. ggplot2가 공백이있는 변수를 받아들이는지 여부를 모르겠다는 것을 제외하고는 처음부터이 작업을 수행했을 것입니다. -i는 작동 할 수있는 `variable`
을 알아 냈습니다. 그래서 나는 변수를 재 명명했다. 그것은 작동합니다 - 킨다. 문제는 그 주위에 따옴표가있는 텍스트가 나타나는 것입니다. 이제 나는 이것들을 제거하는 방법이 필요하다 !!! 누구 아이디어? 감사!
주석을 작성한 다음 툴팁에 다른 변수가 있습니다. 예를 들어이 영역을 이름으로 표시 한 다음 툴팁에 통계를 표시합니다. 나는이 일을하려고 노력했지만 ggplotly는 명시 적으로 텍스트와 레이블을 모두 전달하더라도 항상 툴팁 텍스트로 레이블을 사용합니다. 도움이 될만한 아이디어가 있습니까? – jtanman