I need to convert into widget a simple ggplot, a stacked bar with uncertainty.
The data:
world.tot <- data.frame('country'='world', 'GHG'=c('CH4', 'CO2','N2O'),
'emi'=c(6e+6, 3e+6, 1+6),
'unc.min'=8561406, 'unc.max'=14027350)
and the ggplot:
p2 <- ggplot(world.tot) +
geom_bar(aes(x=country,y=emi,fill=GHG), stat='identity', position='stack' ) +
geom_errorbar(aes(x=country, ymin=unc.min, ymax=unc.max), width=0.2) +
theme(axis.title. x=element_blank(), axis.title. y=element_blank()) +
theme(legend.position='none')
When I try: ggplotly(p2) only the stacked bars are converted, not the error bar. Any advice?
Alternatively, I could use plot_ly to create the plot, but cannot manage to add the error bar:
plot_ly(world.tot, x=~country. y=~emi, color=~GHG,type=bar,
error_y=~list(array(c(unc.min, unc.max))) %>%
layout(barmode='stack')
This produces error bars to all shares of the stacked histogram, while I need only one error at the top of the stacked histogram.
Any help is appreciated
You can prepare a data.frame that has only one error size per group
library(dplyr)
world.err <- world.tot %>%
group_by(country) %>%
summarise(emi = sum(emi), unc.min = 8561406, unc.max = 14027350)
And plot the errors as a separate trace
plot_ly(world.tot) %>%
add_bars(x = ~country, y = ~emi, color = ~GHG, type='bar') %>%
add_trace(x = ~country, y = ~emi, data = world.err,
showlegend = F, mode='none', type='scatter',
error_y = ~list(array = c(unc.min, unc.max), color = '#000000')) %>%
layout(barmode='stack')
Related
I working with plotly in R. I am trying to plot charts with the same colors. Below you can see data and charts.
library(plotly)
library(reshape2)
library(dplyr)
df<-data.frame(city=c("NYC","Seattle","Boston","LA","Seattle"),
value=c(100,200,300,400,500))
df <-melt(df)
Now I am plotting pie chart with colors shown below:
fig<-df %>%
plot_ly(labels = ~city, values = ~value)
fig <- fig %>% add_pie(hole = 0.6)
fig
Finally, I want to plot a bar chart with the same colors as the pie plot, shown above. In order to do this, I tried this command lines :
df <-melt(df)
fig <- plot_ly(df, x = ~city, y = ~value, type = 'bar')
fig
So can anybody help me with how to plot a barplot with the same colors as pie chart ?
Here's a somewhat hacky but effective solution:
fig <- ggplot(df, aes(city, value, fill = city)) +
geom_col() +
scale_fill_manual(values = c("#2ca02c", "#ff7f0e",
"#d62728", "#1f77b4")) +
theme_minimal() +
theme(panel.grid.major.x = element_blank())
ggplotly(fig)
You may use ggplot2 package for both charts and it shall give matching colours to the city in each chart:
#install.packages('ggplot2')
library(ggplot2)
library(dplyr)
#your data frame
df<-data.frame(city=c("NYC","Seattle","Boston","LA","Seattle"),
value=c(100,200,300,400,500))
df <-melt(df)
PieChart <- df %>% ggplot(aes(x="", y=value, fill=city)) +
geom_bar(stat = "identity", width=1) +
coord_polar("y",start=0) +
theme_void()
PieChart
The resulted plot:
BarChart <- df %>% ggplot(aes(x=city, y=value, fill=city)) +
geom_bar(stat = "identity") +
theme_void() +
xlab("City") + ylab("Value")
BarChart
The resulted plot:
You may find this helpful.
In addition, here's what you can do if you want to completely use plotly.
#install.packages('RColorBrewer')
library(RColorBrewer)
library(dplyr)
library(plotly)
df<-data.frame(city=c("NYC","Seattle","Boston","LA","Seattle"),
value=c(100,200,300,400,500))
df <-melt(df)
df
#define a vector with colour palette of four colours
cols <- brewer.pal(12, "Set3")[1:4]
cols
Bar Chart:
BarChart <- df %>% plot_ly(x = ~city, y = ~value, showlegend = TRUE,
type = 'bar', color = ~city, colors = cols)
BarChart
The resulting plot:
Pie Chart:
PieChart <- df %>% plot_ly(labels = ~city, values = ~value,
marker = list(colors = cols), showlegend = TRUE) %>%
add_pie(hole = 0.6)
PieChart
The resulting plot:
Note that this would still give you different colours for the cities, so another suggestion that I have besides the linked answer above is to try creating a new data frame that has the unique four cities i.e no repetition, the summation of the values for the repeated cities, and bind another column that has a unique colour per city.
My data consists of a date variable and four numeric variables, of the 4 numeric variables I wish to plot two of these as a stacked bar chart and the remaining variables as line charts.
Is it possible to create two line charts and a stacked bar chart in a single plot using ggplot?
My data looks as follows:
data <- tibble(Month = 1:12,Brands = c(1,1,1,1,1,1,2,2,2,2,2,2),Generics = Brands + 1,Metric1 = c(5,5,5,5,5,5,6,6,7,8,9,10),Metric2 = c(10,10,11,11,12,13,14,15,16,17,18,19))
I wish to plot months on the x axis, Brands1 & Brands2 as stacked bar charts and Metric1 & Metric2 as line charts all on the same chart if possible.
Something like this?
library(tidyverse)
data <- tibble(Month = 1:12,Brands = c(1,1,1,1,1,1,2,2,2,2,2,2),Generics = Brands + 1,Metric1 = c(5,5,5,5,5,5,6,6,7,8,9,10),Metric2 = c(10,10,11,11,12,13,14,15,16,17,18,19))
data %>%
pivot_longer(cols = c(Brands,Generics)) %>%
pivot_longer(cols = c(Metric1,Metric2),
names_to = "metric_name",values_to = "metric_value") %>%
ggplot(aes(Month))+
geom_col(aes(y = value, fill = name))+
geom_line(aes(y = metric_value, col = metric_name),size = 1.25)+
scale_x_continuous(breaks = 1:12)+
scale_color_manual(values = c("black","purple"))
I want to create a Vertical Likert Line Chart , is there anyway to plot it by ggplot2 or highcharter ?
here is the example chart:
data example :
value1 <- abs(rnorm(26))*2
data <- data.frame(
x=LETTERS[1:26],
value1=value1,
value2=value1+1+rnorm(26, sd=1)
)
library(tidyverse)
data %>%
pivot_longer(-x) %>%
ggplot(aes(x, value, color = name, group = name)) +
geom_line() +
geom_point() +
coord_flip()
P.S. --- Since ggplot2 3.3.0 from March 2020, you can skip the "coord_flip" step and describe the axes directly in the orientation you want them, but the geom_line step still needs a nudge to display correctly:
...
ggplot(aes(value, x, color = name, group = name)) +
geom_line(orientation = "y") +
geom_point()
so I'm trying to Plot chart. I filtered the original dataset Datengf to get the median income of each year (MULTYEAR) and the variable Schulbildung. No chart looks like this: chart. Now I want to plot chart by using ggplot and geom_line. On the x-axis MULTYEAR and on the y-axis the medianincome. But I want it to be a different line and color for each value of Schulbildung.
Chart code:
chart <- Datengf %>%
filter(SEX == 1)%>%
group_by(MULTYEAR,Schulbildung) %>%
summarise(medianincome = median(INCWAGE))%>%
ungroup()%>%
mutate(Schulbildung = ifelse(Schulbildung < 12, "others", Schulbildung)) %>%
group_by(Schulbildung,MULTYEAR)%>%
summarise(medianincome = sum(medianincome))
I tried using
chartplot <- chart %>%
ggplot(aes(x = MULTYEAR, y = medianincome))+
geom_line()
but the chart is an complete mess.
Specify color in the aes function:
chartplot <- chart %>%
ggplot(aes(x = MULTYEAR, y = medianincome, color = Schulbildung))+
geom_line()
I have a plot that I am rendering in shiny using plotly and ggplot2. However, I do not want the option bar that appears on hover to appear. Is there a way to use ggplotly(p) and remove the option bar?
There is a great answer on community plotly the short version:
library(plotly)
set.seed(100)
d <- diamonds[sample(nrow(diamonds), 1000), ]
Using ggplotly:
p <- ggplot(d, aes(carat, price)) + geom_point()
ggplotly(p) %>% config(displayModeBar = F)
If you are not using ggplotly you can do:
plot_ly(d, x = carat, y = price, text = paste("Clarity: ", clarity),
mode = "markers", color = carat, size = carat) %>% config(displayModeBar = F)