R: How to customize plotly interactive hover window? - r

Here I have interactive barplot given by ggplotly. The only issue is that when I move mouse around bars, in the "model" category there is strange number instead of A or B (see the picture). Is it possible to customize plotly popup windows?
df <- data.frame (model = c("A", "A","B","B"),
year = c("2022","2021","2022","2021"),
sale = c(350,170,300,150),
change = c(180,NA,150,NA),
percent = c(105.8,NA,100,NA),
info = c("180, 105.8%",NA,"300,100%",NA)
)
#ggplot
plot <- ggplot(df, aes(fill=year, y=model, x=sale)) +
geom_bar(position="dodge", stat="identity") + geom_text(aes(label=info, x=1.11*max(sale),), fontface='bold')+ xlim(0, 1.2*max(df$sale)) +
theme(legend.position="bottom")+labs(fill = " ")+
scale_fill_brewer(palette = "Paired")
ggplotly(plot)

Personally, i avoid using ggplotly() as it more often than not formats the visuals in a way that i do not want.
A full plotly approach could look like this:
plot_ly(
data = df,
x = ~sale,
y = ~model,
color = ~year,
text = ~year,
type = "bar") %>%
add_trace(
x = ~max(df$sale) * 1.1,
y = ~model,
type = 'scatter',
mode = 'text',
text = ~info,
showlegend = FALSE
) %>%
style(hovertemplate = paste("Sale: %{x}",
"Model: %{y}",
"Year: %{text}",
sep = "<br>"))
You could also try to append the style() object to your ggplotly() object. I am not sure if this will work however.

For some reason, it works better if you use x=model and flip the axes:
plot <- ggplot(df, aes(fill=year, x=model, y=sale)) +
geom_bar(position="dodge", stat="identity") + geom_text(aes(label=info,y=1.11*max(sale),), fontface='bold')+
ylim(0, 1.2*max(df$sale)) +
theme(legend.position="bottom")+labs(fill = " ")+
scale_fill_brewer(palette = "Paired")+
coord_flip()
ggplotly(plot)

Related

Why does the order of my tooltips change when using ggplotly?

When I add my own text into the tooltips for ggplotly the order of the text changes. In my example the order is Portuegese then German in my text, however on the plot the order is different.
How do I fix this?
Thanks
library(ggplot2);
library(plotly);
language <- c("de","es","hi","pt","sv","en")
averageRating <- c(6,4,3,9,10,30)
my_data <- data.frame(language, averageRating)
text <- c("Portuegese","German","Spanish","Hindi","Swedish","English")
p <- ggplot(data=my_data, aes(x=language, y=averageRating, text = text)) +
geom_bar(stat = "identity")
ggplotly(p, tooltip = c("text"))
Maybe you should try factor within aes when applying ggplot, e.g.,
ggplot(data = my_data, aes(x = factor(language, levels = language), y = averageRating, text = text))
To fix this I added a new column in my dataframe with the name I wanted displayed in the tooltip. I then set the "text" inside the aesthetic to that column name.
library(ggplot2);
library(plotly);
my_data <- read.csv("languageVsRating.csv")
p <- ggplot(data=my_data, aes(x=reorder(language,-averageRating), y=averageRating, text = fullName)) +
geom_bar(stat = "identity") +
labs(x="Film Language",y="Average Rating")+
geom_hline(aes(yintercept = 6.9,linetype="Mean"),colour='red',size=1)+
geom_hline(aes(yintercept = 6.7,linetype="Median"),colour='blue',size=1)+
scale_linetype_manual(name = "",values = c(2,2),guide = guide_legend(override.aes = list(color = c("red", "blue"))))
ggplotly(p, tooltip = c("text","y"))
There is another column inside languageVsRating.csv with the language full name

Adding a single label per group in ggplot with stat_summary and text geoms

I would like to add counts to a ggplot that uses stat_summary().
I am having an issue with the requirement that the text vector be the same length as the data.
With the examples below, you can see that what is being plotted is the same label multiple times.
The workaround to set the location on the y axis has the effect that multiple labels are stacked up. The visual effect is a bit strange (particularly when you have thousands of observations) and not sufficiently professional for my purposes. You will have to trust me on this one - the attached picture doesn't fully convey the weirdness of it.
I was wondering if someone else has worked out another way. It is for a plot in shiny that has dynamic input, so text cannot be overlaid in a hardcoded fashion.
I'm pretty sure ggplot wasn't designed for the kind of behaviour with stat_summary that I am looking for, and I may have to abandon stat_summary and create a new summary dataframe, but thought I would first check if someone else has some wizardry to offer up.
This is the plot without setting the y location:
library(dplyr)
library(ggplot2)
df_x <- data.frame("Group" = c(rep("A",1000), rep("B",2) ),
"Value" = rnorm(1002))
df_x <- df_x %>%
group_by(Group) %>%
mutate(w_count = n())
ggplot(df_x, aes(x = Group, y = Value)) +
stat_summary(fun.data="mean_cl_boot", size = 1.2) +
geom_text(aes(label = w_count)) +
coord_flip() +
theme_classic()
and this is with my hack
ggplot(df_x, aes(x = Group, y = Value)) +
stat_summary(fun.data="mean_cl_boot", size = 1.2) +
geom_text(aes(y = 1, label = w_count)) +
coord_flip() +
theme_classic()
Create a df_text that has the grouped info for your labels. Then use annotate:
library(dplyr)
library(ggplot2)
set.seed(123)
df_x <- data.frame("Group" = c(rep("A",1000), rep("B",2) ),
"Value" = rnorm(1002))
df_text <- df_x %>%
group_by(Group) %>%
summarise(avg = mean(Value),
n = n()) %>%
ungroup()
yoff <- 0.0
xoff <- -0.1
ggplot(df_x, aes(x = Group, y = Value)) +
stat_summary(fun.data="mean_cl_boot", size = 1.2) +
annotate("text",
x = 1:2 + xoff,
y = df_text$avg + yoff,
label = df_text$n) +
coord_flip() +
theme_classic()
I found another way which is a little more robust for when the plot is dynamic in its ordering and filtering, and works well for faceting. More robust, because it uses stat_summary for the text.
library(dplyr)
library(ggplot2)
df_x <- data.frame("Group" = c(rep("A",1000), rep("B",2) ),
"Value" = rnorm(1002))
counts_df <- function(y) {
return( data.frame( y = 1, label = paste0('n=', length(y)) ) )
}
ggplot(df_x, aes(x = Group, y = Value)) +
stat_summary(fun.data="mean_cl_boot", size = 1.2) +
coord_flip() +
theme_classic()
p + stat_summary(geom="text", fun.data=counts_df)

Why do I get null graphs with ggplot?

Why when I use facet_wrap with the function below, I get null outputs for the graphs. A picture of what is going on is attached. The graphs are smushed together and I cannot see any data points. What is going on?
I have covered up titles because of confidential.
There will be 100 or so facets.
data <- read.csv(data.csv)
data$DateTime <- as.POSIXct(data$DateTime,format ='%m/%d/%Y %r')
data <- data %>% mutate(Person = ifelse(Person == 1, "Person 1", "Person 2"))
data %>%
filter(Size %in% c('S','M')
) %>%
arrange(LargePerson) %>%
ggplot(aes(x = DateTime,y = Price)) +
geom_point(
aes(colour = Person)) +
scale_colour_manual(values = c("Person 1" = "blue", "Person 2" = "black")) +
facet_wrap(~ID,scales = "free",ncol=2) + labs(x = "Date") +
scale_x_datetime(breaks = date_breaks("2 days"),labels = date_format("%m/%d/%y")) +
theme(axis.text.x = element_text(angle = 90,vjust = 0.5),
legend.position="bottom"
)
I did some playing around and I think it's probably what I said in my comment: 100 facets just don't fit into the space allotted for the image. The thing that gets squished is the graph itself, not the surrounding elements (like titles etc). For example, here's some fake plots at 50 facets:
df<-data.frame(group=apply(expand.grid(LETTERS,LETTERS),1,paste0,collapse="")[1:50],x=runif(50),y=runif(50))
ggplot(df, aes(x,y))+geom_point() +facet_wrap(~group,ncol=2)
And here's the same plot, with the same code, with the height of the PNG set to 10,000:

R: placing a text with combination of variables over bars in ggplot

Lets draw a bar chart with ggplot2 from the following data (already in a long format). The values of the variable are then placed in the middle of the bars via geom_text() directive.
stuff.dat<-read.csv(text="continent,stuff,num
America,apples,13
America,bananas,13
Europe,apples,30
Europe,bananas,21
total,apples,43
total,bananas,34")
library(ggplot2)
ggplot(stuff.dat, aes(x=continent, y=num,fill=stuff))+geom_col() +
geom_text(position = position_stack(vjust=0.5),
aes(label=num))
Now it is necessary to add on top of the bars the "Apple-Bananas Index", which is defined as f=apples/bananas - just as manually added in the figure. How to program this in ggplot? How it would be possible to add it to the legend as a separate entry?
I think that the easiest way to achieve this is to prepare the data before you create the plot. I define a function abi() that computes the apple-banana-index from stuff.dat given a continent:
abi <- function(cont) {
with(stuff.dat,
num[continent == cont & stuff == "apples"] / num[continent == cont & stuff == "bananas"]
)
}
And then I create a data frame with all the necessary data:
conts <- levels(stuff.dat$continent)
abi_df <- data.frame(continent = conts,
yf = aggregate(num ~ continent, sum, data = stuff.dat)$num + 5,
abi = round(sapply(conts, abi), 1))
Now, I can add that information to the plot:
library(ggplot2)
ggplot(stuff.dat, aes(x = continent, y = num, fill = stuff)) +
geom_col() +
geom_text(position = position_stack(vjust = 0.5), aes(label = num)) +
geom_text(data = abi_df, aes(y = yf, label = paste0("f = ", abi), fill = NA))
Adding fill = NA to the geom_text() is a bit of a hack and leads to a warning. But if fill is not set, plotting will fail with a message that stuff was not found. I also tried to move fill = stuff from ggplot() to geom_col() but this breaks the y⁻coordinate of the text labels inside the bars. There might be a cleaner solution to this, but I haven't found it yet.
Adding the additional legend is, unfortunately, not trivial, because one cannot easily add text outside the plot area. This actually needs two steps: first one adds text using annotation_custom(). Then, you need to turn clipping off to make the text visible (see, e.g., here). This is a possible solution:
p <- ggplot(stuff.dat, aes(x = continent, y = num, fill = stuff)) +
geom_col() +
geom_text(position = position_stack(vjust = 0.5), aes(label = num)) +
geom_text(data = abi_df, aes(y = yf, label = paste0("f = ", abi), fill = NA)) +
guides(size = guide_legend(title = "f: ABI", override.aes = list(fill = 1))) +
annotation_custom(grob = textGrob("f: ABI\n(Apple-\nBanana-\nIndex",
gp = gpar(cex = .8), just = "left"),
xmin = 3.8, xmax = 3.8, ymin = 17, ymax = 17)
# turn off clipping
library(grid)
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)

R and ggplot: Putting x-axis labels outside the panel in ggplot

Is there a way to bring the labels forward with respect to plot panel in ggplot?
Actually I was trying to answer my question here. I have not got any satisfactory response to that one although I thought it would be possible in ggplot. This is an attempt to get a solution although a hacky one. But the labels are rendered below the plot panel here.
Following are my (example) data, attempted solution and the resulting plot.
library(ggplot2)
library(magrittr)
mydata = data.frame(expand.grid(Tag = c('A','B','C'),Year = 2010:2011,PNo = paste0("X-",1:4)),Value = round(runif(24,1,20)))
mydata$dist = ifelse(mydata$Tag == 'A',0,ifelse(mydata$Tag=='B',2,7))
mydata %>% ggplot(aes(x = dist,y = Value,fill = factor(Year))) +geom_bar(stat='summary',position = 'dodge',fun.y='mean',width = 1) +
facet_wrap(~PNo,ncol=2) +
theme(axis.text.x = element_blank(),axis.ticks.x = element_blank()) +
geom_label(data = mydata %>% filter(PNo %in% c('X-3','X-4')),aes(x = dist,y=0,label = Tag),size=6,inherit.aes=F,color = 'red')
You have to turn off clipping of the bottom panel elements:
p <- mydata %>% ggplot(aes(x = dist,y = Value,fill = factor(Year))) +geom_bar(stat='summary',position = 'dodge',fun.y='mean',width = 1) +
facet_wrap(~PNo,ncol=2) +
theme(axis.text.x = element_blank(),axis.ticks.x = element_blank()) +
geom_label(data = mydata %>% dplyr::filter(PNo %in% c('X-3','X-4')),aes(x = dist,y=0,label = Tag),size=6,inherit.aes=F,color = 'red')
library(grid)
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[grep("panel-2-\\d+", gt$layout$name)] <- "off"
grid.draw(gt)
See Point clipped on x-axis in ggplot

Resources