I developed a Shiny app with ggplot, and for technical reasons I have to keep on using it. I am trying to create a tooltip system, as the one you can see over here with Dygraphs : http://dygraphs.com/gallery/#g/plotter
There is a solution suggested on SOF, but I don't like it as it's not as user-friendly as the previous one : ToolTip when you mouseover a ggplot on shiny.
Here, there's an area under the plot in which the value is printed, and you have to click over to get the value.
library(ggplot2)
a <- ggplot(data = economics, aes(x = date, y = unemploy))
a <- a + geom_line()
a
My question is : how to develop the same tooltip as Dygraph with ggplot ?
Thanks a bunch ;)
Related
I created a plot using ggplot() and turned off linetype part of the legend using "+ guides(linetype=False)".
However, when I use the ggplotly() function it completely overrides this and still displays the linetype in the legend. My thought was I need to remove that part of the legend for the generated plotly object, but I wasn't sure how to just remove the linetype within the plotly object (p object below). I do want to keep the color legend.
An example dataset to be plotted:
library("ggplot2")
library("plotly")
dataset = read.csv("file_loc")
g = ggplot(data=dataset) +
geom_line(x=dataset$Time,
y=dataset$Values,
group=dataset$group,
linetype=dataset$group,
color=dataset$Othervalue) +
# Doesn't work when using ggplotly function
guides(linetype=FALSE)
p = ggplotly(g)
Note: I am using R version 3.6.0, ggplot2 3.3.5, plotly 4.9.4.1
I found that if I turn off the legend for p or p=layout(p,showlegend=FALSE) it turns off just the plotly legends but keeps the ggplot legends previously hidden or kept.
Trying to add some note at the bottom of R plot graph, to show what colour and line type has been used in the graph that represents for. Tried use mtext() function, but that only allow me to add text, not the line type. Any way to add those colour note?
Try this approach. Please next time follow the advice of #AllanCameron. There are plenty of people wanting to help you, but we need to analyse your data to discover the issue. Here I have used some dummy data similar to that showed in the plot. Next an approach for your issue. If you want a description of your data series you can add a legend. This can be done also using ggplot2 which is used in actual code:
library(ggplot2)
#Data
df <- data.frame(Year=2000:2009,
Positive=runif(10,0,1),
Negative=runif(10,-1,0),
Average=cumsum(seq(-0.5,1,length.out = 10)))
#Plot
ggplot(df,aes(x=factor(Year),y=Positive))+
geom_line(aes(y=Average,group=1,color='Average'),size=1)+
geom_bar(stat='identity',color='black',aes(fill='Positive'),show.legend = T)+
geom_bar(aes(y=Negative,fill='Negative'),stat='identity',color='black')+
scale_fill_manual("",values=c('red','blue','white'))+
scale_color_manual("",values = 'black')+
theme_bw()+
theme(legend.position = 'bottom',legend.key.height = unit(0.001,'mm'))+
xlab('Year')+labs(caption = 'Your Info')
Output:
You can play around key size to make it thinner.
I try to generate a plot on which every point stands for an event. Color, Size and faced_grid are used to give additional information available in a visual way. The graph is working in ggplot2 but it is often important to know the exact numbers so an interactive version is needed which enables to hover over the point and get the info. I tried to convert the plot into an interactive version with the function ggplotly from the plotly-package. The problem then is, that the legend not only display the different states of the used attributes, it contains every existent combination. In addition, it did not display info from geom_rect.
I found related/similar questions but they used the function plot_ly and not ggploty or did not provide an answer.
Following, the same problem illustrated with the mtcars dataset:
library(plotly)
g = ggplot(mtcars,aes(x=mpg,y=disp,color = as.factor(cyl),size =as.factor(gear))) +
geom_point() +
geom_text(label = c(rep("A",nrow(mtcars)-5),rep("B",5)),color = "black",size=4) +
geom_rect(data=data.frame(name="zone",Start=20,End = 30,ymin = -Inf,ymax = Inf),aes(xmin=Start, xmax=End, ymin=ymin, ymax=ymax,fill=name),inherit.aes = FALSE,alpha=0.3)+
facet_grid(vs~am)
g
This is the result and how it should look like: ggplot Graph
Now using ggplotly
ggplotly(g)
This is the result: ggploty Graph
(1) The legend is now a combination of the different attributes used for Color and Size
(2) geom_rect is in the legend but didn’t get displayed in the graph
Does anyone knows how to get the same graph in ggplotly like in ggplot2? I am grateful for every hint. Thanks
Dave
I do not know how to fix the combination of legends when you use ggplotly. But, I can fix the second problem, if you do not use the Inf and -Inf, the geom_rect will work:
ggplotly(ggplot(mtcars,aes(x=mpg,y=disp, = as.factor(cyl),size =as.factor(gear))) +
geom_rect(aes( xmin=20,
xmax=30,
ymin=0,
ymax=max(mtcars$disp),
fill="Name"),
inherit.aes = FALSE, alpha=0.3) +
geom_point() +
geom_text(label = c(rep("A",nrow(mtcars)-5),rep("B",5)), = "black",size=4) +
facet_grid(vs~am))
However, the legends are bad.
I would suggest using subplot to create the same thing in Plotly, and I think this link Ben mentioned will help you create each subplot. One thing to mention is that I had trouble Illustrating different size in legend in plotly, while the size of the marker will be different, there will not be a legend for the size scale. Maybe a scale will be a better option.
Attempting to create pie chart with ggplot2 but cannot seem to get it using other references online. The chart I create is missing most of its fill.
ggplot(sae,aes(x=1,fill=factor(State), width=1))+
geom_bar()+
ggtitle("House by State")+
coord_polar(theta='y')
This code gives:
How do I fill the center?
Any other improvements appreciated.
With sample data
sae <- data.frame(State=sample(LETTERS[1:6],60,T))
ggplot(sae,aes(x=factor(1),fill=factor(State)))+
geom_bar(width=1)+
ggtitle("House by State")+
coord_polar(theta="y")
EDIT: Other options (because piecharts are bad)
#following Jaaps example: some better way to visualize this
#grouped barchart
p1 <- ggplot(sae, aes(x=State, fill=State)) +
geom_bar() + labs(title="grouped barchart")
#stacked barchart; especially practical if you want to compare groups
sae$group <- rbinom(60,1,0.5)
p2 <- ggplot(sae, aes(x=factor(group),fill=State))+
geom_bar(width=0.5) + labs(title="grouped stacked barchart")
do.call(grid.arrange,list(grobs=list(p1,p2),ncol=2))
As #Heroka already mentioned in the comments, pie-charts are a bad way of visualizing information. They are bad that it is even mentioned in the help-files of R.
From ?pie:
Pie charts are a very bad way of displaying information. The eye is
good at judging linear measures and bad at judging relative areas. A
bar chart or dot chart is a preferable way of displaying this type of
data.
Cleveland (1985), page 264: “Data that can be shown by pie charts
always can be shown by a dot chart. This means that judgements of
position along a common scale can be made instead of the less accurate
angle judgements.” This statement is based on the empirical
investigations of Cleveland and McGill as well as investigations by
perceptual psychologists.
Some further reading on the pie-chart debate.
With the example data of #Heroka:
ggplot(sae,aes(x = factor(1), fill = factor(State)))+
geom_bar(width = 1, position = "dodge")+
ggtitle("House by State")
you get:
A clear demonstration that it's better to see the differences between the categories when you use a barchart instead of a piechart.
When you want to show information about proportions, there is another choice, the waffle package which gets back more to what you probably intend to show with a pie chart (i.e., proportions). In most instances, the bar plots above would likely be best, but for the sake of showing another way of plotting...
Using the sae data from above:
library(waffle) # install the package if you don't have it
w <- table(sae)
w.waf <- waffle(table(sae))
w.waf + ggtitle("Contextless Waffle Graph") + theme(plot.title=element_text(face="bold", size=24))
which yields this:
When I try graphing my double bar graph using ggplot on r- shiny using plotly's interface, the double bar graph is getting data from random places and does not properly graph the plot.
I have used this interface on single bar plots and it has worked perfectly and correctly displays the data and graph but with double bar plots, it just uses random data? The regular ggplot plot is fine but when I transform it to a plotly it screws up.
output$tvpn <- renderPlotly({
mpolfinal4$Partner.Name= factor(mpolfinal4$Partner.Name,
levels=names(sort(table(mpolfinal4$Partner.Name),
decreasing=TRUE)))
tv = ggplot(mpolfinal4, aes(x =Partner.Name, fill=Domain.Name)) +
geom_bar(colour="black") +
theme(axis.text.x=element_text(angle=90,hjust=1)) +
ggtitle("Total Views by Partner Name, and Domain Name")
out = ggplotly(tv)
out
})
Has anyone experienced this issue/know how to fix it?