I need create treemap plot,here my data
fd=structure(list(geo = c("FR", "ID", "KE", "NG", "TH", "BD", "BF",
"GB", "ID", "US", "DE", "FR", "GB", "US", "ZA", "BR", "DE", "GB",
"IN", "US", "GB", "KR", "TR", "US", "UZ", "AU", "CA", "CI", "SN",
"TH", "DE", "FR", "GB", "ID", "US", "DE", "GB", "SA", "TH", "US"
), total_gross = c(74800.179198648, 206351.720296498, 87104.011204479,
230279.098284536, 88416.123570242, 30649.923307, 1797.704424,
7203.416263, 20182.697799371, 1248.065576, 3884.255650215, 4312.951986,
7590.300395, 6124.868841, 5223.464131, 8579.658550893, 5105.435153999,
7041.527072472, 28142.360652436, 34010.800759784, 2274.429363,
1032.775403, 3611.359533, 27850.021229, 21428.923638, 3428.672326,
1596.969545249, 3506.142073, 1137.637902, 1932.374443, 7028.91663786,
9778.256711419, 6271.898612455, 5442.656310329, 47786.935549483,
46615.8912281, 34268.94122415, 20576.448612643, 20973.696070114,
288397.397156119), dataset = c("data0_50", "data0_50", "data0_50",
"data0_50", "data0_50", "data10001_50000", "data10001_50000",
"data10001_50000", "data10001_50000", "data10001_50000", "data1001_5000",
"data1001_5000", "data1001_5000", "data1001_5000", "data1001_5000",
"data101_500", "data101_500", "data101_500", "data101_500", "data101_500",
"data50001", "data50001", "data50001", "data50001", "data50001",
"data5001_10000", "data5001_10000", "data5001_10000", "data5001_10000",
"data5001_10000", "data501_1000", "data501_1000", "data501_1000",
"data501_1000", "data501_1000", "data51_100", "data51_100", "data51_100",
"data51_100", "data51_100"), perc = c(4L, 8L, 18L, 14L, 5L, 3L,
4L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L,
1L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 2L, 3L,
1L, 2L)), class = "data.frame", row.names = c(NA, -40L))
I create treemap, it's important for me to see the perc
in each category of the dataset + geo subcategories of this dataset.
I do this
fd=read.csv("C:/1/fd.csv",dec=".",sep=";")
str(fd)
# Install required packages if they are not installed
if (!require(treemap)) {
install.packages("treemap")
library(treemap)
}
# Convert data into a format suitable for treemap
treemap_data <- aggregate(perc ~ geo + dataset, data = fd, sum)
# Building treemap
treemap(treemap_data, index=c("geo", "dataset"), vSize="perc", vColor="perc")
------------------------
# Set image size
png("treemap.png", width = 3200, height = 2400, res = 1000) # Change the size and resolution to your liking
# Building treemap
treemap(treemap_data, index=c("geo", "dataset"), vSize="perc", vColor="perc")
# Closing the output device
dev.off()
as the result i get completely unreadable chart
the names of geo and datasets are mixed up, and there is no exact value for the metric perс
variable
How to do that each square contains the name of the geo, the name of the dataset and its perc
value
example like this
here is the dataset, geo and perс value. Thank you for your help.
You could paste
the 3 elements together with a line-break between them:
library(treemap)
treemap_data <- aggregate(perc ~ geo + dataset, data = fd, sum) |>
dplyr::mutate(label = paste0(geo, "\n", dataset, "\n", perc))
treemap(treemap_data, index = "label", vSize="perc", vColor="perc",
fontsize.labels = 6, align.labels = c("left", "top"))