Circular Heatmap That Looks Like a Donut

Circular Heatmap that looks like a donut

Here is a solution accomplished by (1) converting factor to numeric and adding an offset, (2) manually specifying y-limits and (3) manually setting y-axis breaks and labels:

library(reshape)
library(ggplot2)
# Using ggplot2 0.9.2.1

nba <- read.csv("http://datasets.flowingdata.com/ppg2008.csv")
nba$Name <- with(nba, reorder(Name, PTS))
nba.m <- melt(nba)
nba.m <- ddply(nba.m, .(variable), transform, value = scale(value))

# Convert the factor levels to numeric + quanity to determine size of hole.
nba.m$var2 = as.numeric(nba.m$variable) + 15

# Labels and breaks need to be added with scale_y_discrete.
y_labels = levels(nba.m$variable)
y_breaks = seq_along(y_labels) + 15

p2 = ggplot(nba.m, aes(x=Name, y=var2, fill=value)) +
geom_tile(colour="white") +
scale_fill_gradient(low = "white", high = "steelblue") +
ylim(c(0, max(nba.m$var2) + 0.5)) +
scale_y_discrete(breaks=y_breaks, labels=y_labels) +
coord_polar(theta="x") +
theme(panel.background=element_blank(),
axis.title=element_blank(),
panel.grid=element_blank(),
axis.text.x=element_blank(),
axis.ticks=element_blank(),
axis.text.y=element_text(size=5))

ggsave(filename="plot_2.png", plot=p2, height=7, width=7)

Sample Image

How to add another legend to circular heat map in R

It's not entirely clear to me what you're looking for but this may be it.
You were originally using scale_y_discrete(breaks=y_breaks, labels=y_labels) to project to a continuous variable,var2, in aes(x=Name, y=var2, fill=value). By changing that to scale_y_continuous(breaks=y_breaks, labels=y_labels) you can get the categorical labels listed for nba.m$variable.

ggplot(nba.m, aes(x=Name, y=var2, fill=value)) +
geom_tile(colour="white") +
geom_text(data=nba.labs, aes(x=Name, y=var2+1.5,
label=Name, angle=ang, hjust=hjust), size=3) +
scale_fill_gradient(low = "white", high = "steelblue") +
ylim(c(0, max(nba.m$var2) + 1.5)) +
scale_y_continuous(breaks=y_breaks, labels=y_labels) +
coord_polar(theta="x") +
theme(panel.background=element_blank(),
axis.title=element_blank(),
panel.grid=element_blank(),
axis.text.x=element_blank(),
axis.ticks=element_blank(),
axis.text.y=element_text(size=5))

Sample Image

UPDATE

I'm not sure what you're trying to do here -those values are not blank in the center because there's data there, removing scale_y_continuous(breaks=y_breaks, labels=y_labels) limits the scale of the y-axis such that the date is no longer graphed. That's why you're not seeing the middle filled when that line of code is removed. At any rate, if that's what you're looking for, what you need to do is delete scale_y_continuous(breaks=y_breaks, labels=y_labels) and turn off the labels for the y-axis, then manually add those labels using grob. I'm sure there's a better way to accomplish what you need but this will get you started at least.

p<-ggplot(nba.m, aes(x=Name, y=var2, fill=value)) +
geom_tile(colour="white") +
geom_text(data=nba.labs, aes(x=Name, y=var2+1.5,
label=Name, angle=ang, hjust=hjust), size=2.5) +
scale_fill_gradient(low = "white", high = "steelblue") +
ylim(c(0, 50)) +
coord_polar(theta="x") +
theme(panel.background=element_blank(),
axis.title=element_blank(),
panel.grid=element_blank(),
axis.text.x=element_blank(),
axis.ticks=element_blank(),
axis.text.y=element_text(size=5))+ theme(axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank())
lab = textGrob((paste("G MIN PTS FGM FGA FGP FTM FTA FTP X3PM X3PA X3PP ORB DRB TRB AST STL BLK TO PF")),
x = unit(.1, "npc"), just = c("left"),
gp = gpar(fontsize = 7))

gp = ggplotGrob(p)
gp = gtable_add_rows(gp, unit(10, "grobheight", lab), -1)
gp = gtable_add_grob(gp, lab, t = -2, l = gp$layout[gp$layout$name == "panel",]$l)

grid.newpage()
grid.draw(gp)

Sample Image

Circular tree with heatmap

The answer according @xiangpin at GitHub.

Big offset value to geom_tiplabel:

p <- ggtree(beast_tree)  
p1 <- gheatmap(
p, genotype, colnames=TRUE,
colnames_angle=-45,
colnames_offset_y = 5,
colnames_position = "bottom",
width=0.3,
hjust=0, font.size=2) +
scale_fill_manual(breaks=c("HuH3N2", "pdm", "trig"),
values=c("steelblue", "firebrick", "darkgreen"), name="genotype") +
geom_tiplab(align = TRUE, linesize=0, offset = 7, size=2) +
xlim_tree(xlim=c(0, 36)) +
scale_y_continuous(limits = c(-1, NA))
p1

Solution 1

Using ggtreeExtra:

library(ggtreeExtra)
library(ggtree)
library(treeio)
library(ggplot2)

beast_file <- system.file("examples/MCC_FluA_H3.tree", package="ggtree")
genotype_file <- system.file("examples/Genotype.txt", package="ggtree")

tree <- read.beast(beast_file)
genotype <- read.table(genotype_file, sep="\t")

colnames(genotype) <- sub("\\.$", "", colnames(genotype))
genotype$ID <- row.names(genotype)

dat <- reshape2::melt(genotype, id.vars="ID", variable.name = "type", value.name="genotype", factorsAsStrings=FALSE)
dat$genotype <- unlist(lapply(as.vector(dat$genotype),function(x)ifelse(nchar(x)==0,NA,x)))

p <- ggtree(tree) + geom_treescale()

p2 <- p + geom_fruit(data=dat,
geom=geom_tile,
mapping=aes(y=ID, x=type, fill=genotype),
color="white") +
scale_fill_manual(values=c("steelblue", "firebrick", "darkgreen"),
na.translate=FALSE) +
geom_axis_text(angle=-45, hjust=0, size=1.5) +
geom_tiplab(align = TRUE, linesize=0, offset = 6, size=2) +
xlim_tree(xlim=c(0, 36)) +
scale_y_continuous(limits = c(-1, NA))
p2

Solution 2

ggplot donut chart in R

On StackOverflow you are encouraged to show your own efforts to solve the problem. This helps to guide answers and helps to determine the level of explanation you require. Without more details, all I can provide is an example:

library(tidyverse)
devtools::install_github("abresler/nbastatR")
library(nbastatR)
library(ggrepel)

options(scipen = 1000000)

salaries <- nba_insider_salaries(assume_player_opt_out = T,
assume_team_doesnt_exercise = T,
return_message = T)
salaries %>%
group_by(nameTeam) %>%
filter(str_detect(slugSeason, "2020")) %>%
select(nameTeam, value) %>%
summarise(total = sum(value)) %>%
mutate(perc = total / sum(total)) %>%
mutate(ymax = cumsum(perc),
ymin = c(0, head(ymax, n=-1))) %>%
mutate(labelPosition = (ymax + ymin) / 2) %>%
rename(`Team Name` = nameTeam,
`Total Salaries` = total) %>%
ggplot(aes(ymax = ymax, ymin = ymin, xmax = 4, xmin = 3)) +
geom_rect(aes(fill = `Total Salaries`)) +
geom_label_repel(x = 4, aes(y = labelPosition,
label = `Team Name`),
min.segment.length = 0, size = 2, force = 3) +
coord_polar(theta = "y") +
xlim(c(2, 4)) +
ggtitle("NBA Total Salaries", subtitle = "2020 Season") +
scale_fill_continuous(labels = scales::dollar) +
theme_void()

example_1.png

Does this answer your question?

Can i Build Circular heat map using highchart

Circular heatmap is not supported by default in Highcharts, but with additional code you can create polar heatmap chart:

chart: {
type: 'heatmap',
polar: true
}

Live demo: http://jsfiddle.net/BlackLabel/kxznatLf/



Related Topics



Leave a reply



Submit