ggplot2 plotly faceted chart colors in legends - r

I have a ggplot2 graph that is then driving a plotly chart. I am using the stock diamonds data set and am getting some wonkyness in the legends. I want the legend to ONLY show colorizing for the colors. It appears to be trying to take each clarity from each facet and merge into the legend.
How can I get the legend to only show the colors of the the clarities once? Note: I tried removing size already :)
Below is the code and image of what is produced.
d = diamonds %>% sample_frac(.01)
p = d %>%
ggplot(aes(x = carat, y = price)) +
geom_point(aes(size = depth, color = clarity)) +
facet_wrap(~cut)
ggplotly(p)

This ended up being a bug in the plotly rendering of the graphic, which has been resolved in the latest version of plotly. The solution is to terminate your R session, reinstall your plotly packages and then pull in the updated library.

Related

How to fill the polygons of the radar chart in ggradar in R

I'm plotting the radar chart using ggradar and I would like to fill the polygons of the chart with different colours. Does anyone know how to do this?
Also, how to remove the legend? I cannot remove the legend even thought I've added plot.legend=FALSE.
Here is the code I'm using.
ggradar(dg, group.point.size=1,
axis.labels=c("Depression", "Stress", "Anxiety"), label.centre.y=FALSE, plot.legend=FALSE, values.radar =c("","",""))
Thanks very much!
Not a ggradar() solution -look here-, but you can work with ggplot() an ggiraphExtra() packages (data are the famous iris dataset, due you did not provide any data):
library(ggplot2)
library(ggiraphExtra)
radar <- ggRadar(data=iris,aes(group=Species), alpha = 0.3) + # alpha for polygon colors intensity
theme_light() + # white background etc.
theme(legend.position = 'none') # no legend
radar
You can tweak as you want.
And you can save it:
ggsave(filename="radar.png")

How to create multi-color segmented line with ggplotly?

I have the following dataframe:
df <- data.frame(party=c("Democrat", "Democrat", "Democrat", "Republican","Republican","Republican","Republican","Republican","Democrat","Democrat"), value=c(1,4,2,3,5,6,10,9,8,7), year=c(1960:1969))
I am attempting to plot year on the x axis, value on the y axis, and color by party. So the ggplot version is straightforward and it works:
library(ggplot2)
library(plotly)
p<-ggplot(df, aes(x=year, y=value, color=party, group=1))+geom_line()
p
My problem is when trying to convert this to a plotly object using ggplotly.
ggplotly(p)
It seems that the group=1 option in the ggplot aesthetics that forces the line to be a single line that is segmented by color doesn't carry over to ggplotly at all. Can anyone point to a way to fix this? I've searched for a few hours now, to no avail.
I'd suggest swapping out geom_line for geom_segment, which lets you define the appearance of each line segment. It needs the endpoint for each segment, but it survives the translation into plotly, which seems to have a slightly different model than ggplot2 does for dealing with multiple series.
q<-ggplot(df, aes(x=year, y=value,
xend = lead(year), yend = lead(value),
color=party))+
geom_segment() +
scale_x_continuous(breaks = seq(1960, 1970, by = 2))
q
ggplotly(q)

How to keep the format of plot generated by ggplot while converting to plotly by ggplotly?

Assuming generating a scatter plot with ggplot which has the gradient color and shape based on the variables in mtcars built-in database in R .
using the following script adopted from Nathan Day in my previous question:
library(ggplot2)
plot<- ggplot(mtcars, aes(mpg, hp, fill = cyl, size = cyl)) +
geom_point(shape = 21, stroke = 2) +
scale_fill_gradientn(colours = rainbow(7)) +
scale_size(range = c(2,6))
we would get the following plot
Now, when I am trying to convert it to ploty using the following script :
library(plotly)
ggploty (plot)
I would get the following plot which is basically change the color and I loose my gradient colour featur in my ggplot as can be seen in previous plot generated by ggplot.
Of course I would get the interactive image BUT I was not able to upload in this page so I just take a static image of the generated ploty image.
Can you please help me to know how I can keep the original format of ggplot when converting to ploty by ggploty command?

plotly labels in R stacked area chart

I am trying to make a stacked area chart in R exactly like this ggplot2 one (below) only using plotly.
Here is a link to my data.
To generate a plotly version of the above ggplot2 chart, I first have to add the values of each column in my dataframe, elw, on top of the values in the previous column like so. This is because plotly (as far as I'm aware) does not have the ability to automatically stack values in area charts.
With this new stacked data set, elw_stack, I use the following code to make my plotly chart:
el_plot2 = ggplot() +
geom_area(aes(elw_stack$year, elw_stack$x99999, fill = 'green')) +
geom_area(aes(elw_stack$year, elw_stack$x20000, fill = 'red')) +
geom_area(aes(elw_stack$year, elw_stack$x19000, fill = 'blue')) +
geom_area(aes(elw_stack$year, elw_stack$x12018, fill = 'purple')) +
geom_area(aes(elw_stack$year, elw_stack$x10006, fill = 'yellow'))
ggplotly(el_plot2)
That code generates this chart:
The issue is that the plotly labels refer to the cumulative elw_stack values. The green value pictured at year 1999 is actually ~3700 (i.e. 11,365 - 7957). But the description bar says the cumulative value of 11,365. Is there a way to fix this so that the labels aren't cumulative values?
I was having a similar problem and eventually decided not to use ggplotly, but instead i used the plot_ly function. Here is the code I used with your data:
elw <- read.csv("elw.csv")
elw_stack <- read.csv("elw_stack.csv")
plot <- plot_ly(data=elw_stack, x=year, y=x10006, fill="tonexty", mode="lines",
text=round(elw$x10006, 0), hoverinfo='x+text+name', name="x10006")
plot <- add_trace(plot, data=elw_stack, x=year, y=x12018, fill="tonexty", mode="lines",
text=round(elw$x12018,0), hoverinfo='x+text+name', name="x12018")
plot <- add_trace(plot, data=elw_stack, x=year, y=x19000, fill="tonexty", mode="lines",
text=round(elw$x19000,0), hoverinfo='x+text+name', name="x19000")
plot <- add_trace(plot, data=elw_stack, x=year, y=x20000, fill="tonexty", mode="lines",
text=round(elw$x20000,0), hoverinfo='x+text+name', name="x20000")
plot <- add_trace(plot, data=elw_stack, x=year, y=x99999, fill="tonexty", mode="lines",
text=round(elw$x99999,0), hoverinfo='x+text+name', name="x99999")
plot <- layout(plot, yaxis=list(title="Whatever title you wanna use"))
And this is how the final plot looks:
plotly image
What I can't get to work is to add the different traces using a for loop. I wanted to write a function that takes a data frame with an arbitrary number of columns as input and returns the stacked area plot, but for some reason the plot won't show all the traces (only first and last)
Hope it helps...

how to add label ticks to a pie chart created with ggplot2?

I built a pie chart using ggplot2 package but because some of the slices are very small the group labels overlap one another, and the value labels as well. Im looking for a way to get the labels furthere away from the slices and linking the slice and the label with a line.
Im using this data:
a<-c(0.5,0.01,2,50,40,7)
data<-data.frame(a)
data$b<-c("A","B","C","D","E","F")
and I used the following code:
p<- ggplot(data,aes(x=1,y=a,fill=b))
p<- p + geom_bar(stat = "identity",color="black")
p<- p+coord_polar("y")
br<-cumsum(data$a) - data$a/2
p<-p+theme(legend.position = "none",axis.text.x=element_text(color='black',size = 15))+
scale_y_continuous(breaks=br,labels=data$b)+
geom_text(aes(y = a/3 + c(0, cumsum(a)[-length(a)]),
label=a),size=6)
and the resaulted plot is:
and im looking for somthing similar to that one (that I found online):

Resources