I am trying to plot a gantt chart and have created it successfully, but being too many values involved, it is visually not presentable.
What should i add to the below code to add a vertical scroll bar in the chart. Also, how can i assign customized colors to the bar & change the background & font.
df <- read.csv("C:/Users/XXXX/Downloads/XXXX.csv" , stringsAsFactors = F)
df$Timeline.Start <- as.Date(df$Timeline.Start, format = "%m/%d/%Y")
df$Timeline.End <- as.Date(df$Timeline.End, format = "%m/%d/%Y")
df$System.Lease.ID <- as.character(df$System.Lease.ID)
ggplot(df, aes(colour= Timeline.Type, group = System.Lease.ID)) +
geom_segment(aes(x=Timeline.Start, xend=Timeline.End, y=System.Lease.ID, yend=System.Lease.ID), size=3) +
labs(x="Timeline.Start", y="System.Lease.ID", title="Project timeline")
Related
I have a bar chart that I want to make interactive in my R Shiny app using plotly.
When it is rendered as a plot, everything is fine using ggplot:
#Bar plots
ggplot(data = df_continents) +
geom_bar(aes(x=country, y=coal_co2), stat="identity", fill="#2596be") +
scale_y_continuous(labels = number_format())`
But, when I try to use plotly, the format gets messed up in an incredible way:
#Bar chart - Adding Labels
ticklabels <- seq(from=0, to=round(max(df$coal_co2*100000)), by=100000)
ticktexts <- c(0,paste(ticklabels[-1]/1000, " 000", sep=""))
output$bar <- renderPlotly({
df %>%
plot_ly(x =~ country, y = ~ coal_co2, type = "bar", marker = list(color = "#2596be")) %>%
layout(yaxis=list(tickvals = ticklabels,
ticktext = ticktexts
))
})
So, I'm not sure what is the problem here, why do I have those white horizontal lines inside the bars?. How do I get my bar chart plot to look like the first screenshot?.
I have created a stacked bar chart, and I would now like to plot a line on the same graphic, but I can't figure it out. I've added the geom_line() to the ggplot call, but I only end up with the line, not the bar chart.
library(ggplot2)
library(reshape)
# First let's make a toy dataset for our stacked plot/line plot example.
year = c(1,2,3,4,5,6)
stocks = c(2,4,3,2,4,3)
exports = stocks*2
domestic = stocks*3
production = c(15,16,15,16,15,16)
# Make 2 df's: alldata is for stacked bar chart, linedata is for plotting a line on top of it.
alldata = data.frame(year,stocks,exports,domestic)
linedata = data.frame(year,production)
# Make alldata 'long' for the stacking
melteddata = melt(alldata,id.vars="year")
# This works fine: (but hooboy was tricky to figure out the ordering w/ stat="identity" )
plotS1 <- ggplot(melteddata, aes(x=year,y=value,factor=variable,fill=variable,order=-as.numeric(variable)))
plotS1 + geom_bar(stat="identity")
# This plots only the line, not the stacked bar chart :
plotS1 <- ggplot(melteddata)
plotS1 + geom_bar(aes(x=year,y=value,factor=variable,fill=variable,order=-as.numeric(variable)), stat="identity")
plotS1 + geom_line(data=linedata, aes(x=year,y=production))
You were close:
plotS1 <- ggplot(melteddata)
plotS1 + geom_bar(aes(x=year,y=value,factor=variable,fill=variable,
order=-as.numeric(variable)), stat="identity") +
geom_line(data=linedata, aes(x=year,y=production))
So SO nailed getting my graph to work, but now i can't get it to print! The end goal is that i need to automate the updating of these plots, so the ggplot and print calls need to be in a function. When i run this code, each file just contains a gray square.
toyfn <- function(plotdata){
library(ggplot2)
plotS1 <- ggplot(plotdata)
plotS1 + geom_bar(aes(x=year,y=value,factor=variable,fill=variable,
order=-as.numeric(variable)), stat="identity") +
geom_line(data=linedata, aes(x=year,y=production))
ggsave('testprint.png',plotS1)
png(filename='testprint2.png')
print(plotS1)
dev.off()
}
library(ggplot2)
library(reshape)
# First let's make a toy dataset for our stacked plot/line plot example.
year = c(1,2,3,4,5,6)
stocks = c(2,4,3,2,4,3)
exports = stocks*2
domestic = stocks*3
production = c(15,16,15,16,15,16)
# Make 2 df's: alldata is for stacked bar chart, linedata is for plotting a line on top of it.
alldata = data.frame(year,stocks,exports,domestic)
linedata = data.frame(year,production)
# Make alldata 'long' for the stacking
melteddata = melt(alldata,id.vars="year")
toyfn(melteddata)
You are saving a plot with no geoms. The plot with geoms will display on the screen, but not in the file.
Try this:
toyfn <- function(plotdata){
plotS1 <- ggplot(plotdata, aes(year, value, factor = variable, fill = variable)) +
geom_bar(stat="identity", aes(order = -as.numeric(variable))) +
geom_line(data=linedata, aes(x=year,y=production))
ggsave('testprint.png', plot = plotS1)
}
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...
I have a script below to illustrate my question:
temp.df <- data.frame(x=1:100,y=1:100,z=1:100,stringsAsFactors=FALSE)
chart <- ggplot(data=temp.df,aes(x=x,y=y))
chart <- chart + geom_line(aes(colour=z))
chart <- chart + scale_colour_continuous(low="blue",high="red")
chart <- chart + theme(legend.position="bottom")
# so far so good, but I think the legend positioned at bottom with such a small size is a waste of space, so I want to "widen" it using the line below...
chart <- chart + guides(colour=guide_legend(keywidth=5,label.position="bottom"))
# oops, it changed to legend for categorical variable
How can I widen the "continuous variable" legend positioned at bottom?
Instead of function guides() you should use the function theme() and set the legend.key.width=
temp.df <- data.frame(x=1:100,y=1:100,z=1:100,stringsAsFactors=FALSE)
chart <- ggplot(data=temp.df,aes(x=x,y=y))
chart <- chart + geom_line(aes(colour=z))
chart <- chart + scale_colour_continuous(low="blue",high="red")
chart <- chart + theme(legend.position="bottom")
chart <- chart + theme(legend.key.width=unit(3,"cm"))
You can use guide_colourbar instead of guide_legend in your code :
temp.df <- data.frame(x=1:100,y=1:100,z=1:100,stringsAsFactors=FALSE)
chart <- ggplot(data=temp.df,aes(x=x,y=y))
chart <- chart + geom_line(aes(colour=z))
chart <- chart + scale_colour_continuous(low="blue",high="red")
chart <- chart + theme(legend.position="bottom")
chart + guides(colour=guide_colourbar(barwidth=30,label.position="bottom"))