Draws a bar chart
ly_bar( fig, x = NULL, y = NULL, data = figure_data(fig), color = NULL, alpha = 1, position = c("stack", "fill", "dodge"), width = 0.9, hover = FALSE, origin = NULL, breaks = NULL, right = FALSE, binwidth = NULL, lname = NULL, lgroup = NULL, legend = NULL, ... )
fig | figure to modify |
---|---|
x | values or field name for x variable, or if NULL, x-axis will be counts of y |
y | values or field name for y variable, or if NULL, y-axis will be counts of x |
data | an optional data frame, providing the source for inputs x, y, and color properties |
color | color for the glyph - a hex code (with no alpha) or any of the 147 named CSS colors, e.g 'green', 'indigo' - for glyphs with both fill and line properties, see "Handling color" below |
alpha | the alpha transparency of the glyph between 0 (transparent) and 1 (opaque) - if glyph has both fill and color properties, see "Handling alpha" below |
position | either "stack", "fill", or "dodge" (see details) |
width | with of each bar, a value between 0 (no width) and 1 (full width) |
hover | logical - should a hover tool be added to show the value of each bar? |
origin, breaks, right, binwidth | parameters to be used for binning x when it is continuous (not yet implemented) |
lname | layer name |
lgroup | layer group |
legend | either a logical specifying not to plot a legend for this layer (FALSE) or a string indicating the name of the legend entry for this layer (note that when mapping plot attributes to variables in |
... | additional parameters for fine control over fill and line properties (see "Additional parameters" below) |
This function expects one of either x or y to be categorical and the other to be numeric or NULL. The numeric variable is summed for each categorical variable and bars are plotted. If no numeric variable is supplied, the unique values of the categorical variable will be tabulated. Within each categorical variable, if color maps to another grouping variable then the bars are split up. In this case, there are three ways to display the bars with the position
argument. The default, "stack" will stack the bars. The "fill" choice will show the relative proportion for each group within each categorical variable level, stacking the bars. The "dodge" choice will plot the bars for each level of the categorical variable side by side.
The color
parameter is a high-level plot attribute that provides default behavior for coloring glyphs.
When using a glyph that only has line properties, this will be the color of the line.
When using a glyph that has has line and fill properties, this will be the color of the line and the fill, with the alpha level of the fill reduced by 50%.
If full control over fill and line color is desired, the fill_color
and line_color
attributes can be specified explicitly and will override color
.
When color is NULL
and fill_color
or line_color
are not specified, the color will be chosen from the theme.
The alpha
is a high-level plot attribute that sets the transparency of the glyph being plotted.
When using a glyph that only has line properties, this will be the alpha of the line.
When using a glyph that has has line and fill properties, this will be the alpha of the line and the alpha of the fill will be set to 50% of this value.
Individual fill and line alpha can be specified with fill_alpha
and line_alpha
and will override alpha
.
When specifying an input data frame for a layer through the data
argument, columns of data
can be used to specify various plot attributes such as color
, etc. For example, with ly_points(..., data = iris, color = Species)
, the Species
variable is used to determine how to color the points. Here, Species
is "mapped" to the color
attribute. Both continuous and categorical variables can be mapped. In the case of continuous variables, the range is cut into slices and attributes are applied to each interval. The mapping from the values of the variable to the actual plot attributes is determined based on the theme.
fill_color | color to use to fill the glyph with - a hex code (with no alpha) or any of the 147 named CSS colors, e.g 'green', 'indigo' |
fill_alpha | transparency value between 0 (transparent) and 1 (opaque) |
line_color | color to use to stroke lines with - a hex code (with no alpha) or any of the 147 named CSS colors, e.g 'green', 'indigo' |
line_width | stroke width in units of pixels |
line_alpha | transparency value between 0 (transparent) and 1 (opaque) |
line_join | how path segments should be joined together 'miter' 'round' 'bevel' |
line_cap | how path segments should be terminated 'butt' 'round' 'square' |
line_dash | array of integer pixel distances that describe the on-off pattern of dashing to use |
line_dash_offset | the distance in pixels into the line_dash that the pattern should start from |
Other layer functions:
ly_abline()
,
ly_annular_wedge()
,
ly_annulus()
,
ly_arc()
,
ly_bezier()
,
ly_boxplot()
,
ly_contour()
,
ly_crect()
,
ly_curve()
,
ly_density()
,
ly_hist()
,
ly_image_url()
,
ly_image()
,
ly_lines()
,
ly_map()
,
ly_multi_line()
,
ly_oval()
,
ly_patch()
,
ly_points()
,
ly_polygons()
,
ly_quadratic()
,
ly_quantile()
,
ly_ray()
,
ly_rect()
,
ly_segments()
,
ly_text()
,
ly_wedge()
# \donttest{ # count of variety figure() %>% ly_bar(variety, data = lattice::barley) %>% theme_axis("x", major_label_orientation = 90) # total yield per variety figure() %>% ly_bar(variety, yield, data = lattice::barley, hover = TRUE) %>% theme_axis("x", major_label_orientation = 90) # swap axes and add hover figure() %>% ly_bar(yield, variety, data = lattice::barley, hover = TRUE) # stack by year figure() %>% ly_bar(variety, yield, color = year, data = lattice::barley, hover = TRUE) %>% theme_axis("x", major_label_orientation = 90) # proportional bars figure() %>% ly_bar(variety, yield, color = year, data = lattice::barley, position = "fill", width = 1) %>% theme_axis("x", major_label_orientation = 90) %>% set_palette(discrete_color = pal_color(c("red", "blue"))) # swap axes and use different palette figure() %>% ly_bar(yield, variety, color = year, data = lattice::barley, position = "fill") %>% set_palette(discrete_color = pal_color(c("red", "blue"))) # side by side bars figure() %>% ly_bar(variety, yield, color = year, data = lattice::barley, position = "dodge") %>% theme_axis("x", major_label_orientation = 90) # use a different theme figure() %>% ly_bar(variety, yield, color = year, data = lattice::barley, position = "dodge") %>% theme_axis("x", major_label_orientation = 90) # }