1 | initial version |
To create a stacked bar graph with data from GridDB in R, you can use the ggplot2 package. Here are the steps:
install.packages("ggplot2")
library(ggplot2)
library(griddb)
# Connect to the GridDB cluster
cluster <- griddb.connect("your_griddb_host", 31810, "your_username", "your_password")
# Query the data from the desired container
container <- cluster$put_container("container_name")
result_set <- container$query("SELECT column1, column2, column3 FROM container_name")
# Convert the result set to a data frame
data <- as.data.frame(result_set)
ggplot(data, aes(x = column1, y = column2, fill = column3)) +
geom_bar(stat = "identity") +
labs(x = "X axis label", y = "Y axis label", fill = "Legend label") +
theme(legend.position = "bottom") +
guides(fill = guide_legend(ncol = 2))
Explanation of the code:
ggplot()
function creates a new ggplot object, with the data
argument specifying the data frame to use, and the aes()
function specifying the mapping between data and aesthetic attributes.geom_bar()
function adds the bar chart layer to the ggplot object, with the stat = "identity"
parameter specifying that the height of the bars should be the actual data values.labs()
function adds axis and legend labels to the plot.theme()
function sets the position of the legend to the bottom of the plot area.guides()
function specifies the number of columns to use for the legend.