ggplot2-book | ggplot2 : elegant graphics for data analysis | Data Visualization library
kandi X-RAY | ggplot2-book Summary
kandi X-RAY | ggplot2-book Summary
This is code and text behind the ggplot2: elegant graphics for data analysis book. Please help us make it better by contributing!.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of ggplot2-book
ggplot2-book Key Features
ggplot2-book Examples and Code Snippets
Community Discussions
Trending Discussions on ggplot2-book
QUESTION
There are so many ways to define colour scales within ggplot2
. After just loading ggplot2
I count 22
functions beginging with scale_color_*
(or scale_colour_*
) and same number beginging with scale_fill_*
. Is it possible to briefly name the purpose of the functions below? Particularly I struggle with the differences of some of the functions and when to use them.
- scale_*_binned()
- scale_*_brewer()
- scale_*_continuous()
- scale_*_date()
- scale_*_datetime()
- scale_*_discrete()
- scale_*_distiller()
- scale_*_fermenter()
- scale_*_gradient()
- scale_*_gradient2()
- scale_*_gradientn()
- scale_*_grey()
- scale_*_hue()
- scale_*_identity()
- scale_*_manual()
- scale_*_ordinal()
- scale_*_steps()
- scale_*_steps2()
- scale_*_stepsn()
- scale_*_viridis_b()
- scale_*_viridis_c()
- scale_*_viridis_d()
What I tried
I've tried to make some research on the web but the more I read the more I get onfused. To drop some random example: "The default scale for continuous fill scales is scale_fill_continuous()
which in turn defaults to scale_fill_gradient()
". I do not get what the difference of both functions is. Again, this is just an example. Same is true for scale_color_binned()
and scale_color_discrete()
where I can not name the difference. And in case of scale_color_date()
and scale_color_datetime()
the destription says "scale_*_gradient
creates a two colour gradient (low-high), scale_*_gradient2
creates a diverging colour gradient (low-mid-high), scale_*_gradientn
creates a n-colour gradient." which is nice to know but how is this related to scale_color_date()
and scale_color_datetime()
? Looking for those functions on the web does not give me very informative sources either. Reading on this topic gets also chaotic because there are tons of color palettes in different packages which are sequential/ diverging/ qualitative plus one can set same color in different ways, i.e. by color name, rgb, number, hex code or palette name. In part this is not directly related to the question about the 2*22
functions but in some cases it is because providing a "wrong" palette results in an error (e.g. the error"Continuous value supplied to discrete scale
).
Why I ask this
I need to do many plots for my work and I am supposed to provide some function that returns all kind of plots. The plots are supposed to have similiar layout so that they fit well together. One aspect I need to consider here is that the colour scales of the plots go well together. See here for example, where so many different kind of plots have same colour scale. I was hoping I could use some general function which provides a colour palette to any data, regardless of whether the data is continuous or categorical, whether it is a fill or col easthetic. But since this is not how colour scales are defined in ggplot2
I need to understand what all those functions are good for.
ANSWER
Answered 2022-Feb-01 at 18:14This is a good question... and I would have hoped there would be a practical guide somewhere. One could question if SO would be a good place to ask this question, but regardless, here's my attempt to summarize the various scale_color_*()
and scale_fill_*()
functions built into ggplot2
. Here, we'll describe the range of functions using scale_color_*()
; however, the same general rules will apply for scale_fill_*()
functions.
There are 22 functions in all, but happily we can group them intelligently based on practical usage scenarios. There are three key criteria that can be used to define practically how to use each of the scale_color_*()
functions:
Nature of the mapping data. Is the data mapped to the color aesthetic discrete or continuous? CONTINUOUS data is something that can be explained via real numbers: time, temperature, lengths - these are all continuous because even if your observations are
1
and2
, there can exist something that would have a theoretical value of1.5
. DISCRETE data is just the opposite: you cannot express this data via real numbers. Take, for example, if your observations were:"Model A"
and"Model B"
. There is no obvious way to express something in-between those two. As such, you can only represent these as single colors or numbers.The Colorspace. The color palette used to draw onto the plot. By default,
ggplot2
uses (I believe) a color palette based on evenly-spaced hue values. There are other functions built into the library that use either Brewer palettes or Viridis colorspaces.The level of Specification. Generally, once you have defined if the scale function is continuous and in what colorspace, you have variation on the level of control or specification the user will need or can specify. A good example of this is the functions:
*_continuous()
,*_gradient()
,*_gradient2()
, and*_gradientn()
.
We can start off with continuous scales. These functions are all used when applied to observations that are continuous variables (see above). The functions here can further be defined if they are either binned or not binned. "Binning" is just a way of grouping ranges of a continuous variable to all be assigned to a particular color. You'll notice the effect of "binning" is to change the legend keys from a "colorbar" to a "steps" legend.
The continuous example (colorbar legend):
QUESTION
I am using sf
and ggplot2
to read shapefiles as simple features and plot various maps. I have been working through the maps chapter in the ggplot2 book but could not really find an answer to the following issue:
Plotting a map using geom_sf
and labelling its features with geom_sf_text
is a pretty straightforward task.
ANSWER
Answered 2021-Feb-03 at 17:20I believe the issue you are facing is caused by your applying the crop at presentation level / the actual data underlying your ggplot object is not cropped.
I suggest applying the crop at data level, for example via sf::st_crop()
. In this example I am using the values of your xlim and ylim objects to create a bounding box (called crop_factor
for no good reason) to limit the extent of the oz_states
at the data level, by creating a new object called oz_cropped
& continuing in your original workflow.
All the centroids and labels and what not will be much better behaved now.
QUESTION
I have a tidy data frame. I want to use it to plot lines via geom_line()
. The colors that each line should have are specified in one column of the data frame. The labels that each line should have are specified in another column (which is also the group
column). With a data frame like this, what is the most efficient and least error-prone way to specify a plot that has a legend with the right colors and labels?
Here is a minimal example that does what I want:
...ANSWER
Answered 2020-Sep-22 at 13:17Are you just looking for scale_color_identity
? This takes the name of the color in the column and maps it to the color aesthetic. You therefore don't need to specify the color in your ggplot call at all. However, if you want the labels to be species rather than colours, you will need to make sure that you specify the labels
argument:
QUESTION
I was originally going to use a ribbon for this function, but ribbon was giving me an "Aesthetics can not vary within a ribbon" error. I tried a geom_smooth, geom_ribbon, and geom_line, and none of them worked for what I needed. The picture posted below is what I need, except that I would like those square joints between where the line changes colors to be either vertical and flush or round.
I looked at these posts already:
Shade Fill or Cover Area Density Under Curve
R ggplot Issue When Using Group Interaction
I've also tried using groups in a couple different ways. I should mention that I'm extremely new to ggplot2 and R in general, so any help or possible avenues would be appreciated. I've been using these resources, which you might find useful for this problem or for your own:
GGPlot2: Elegant Graphics For Data Analysis
Code:
...ANSWER
Answered 2020-Jun-09 at 15:04Here's something that looks a little better with geom_path
, but it seems to ignore the linejoin =
argument when the colors change:
QUESTION
Based on the example below, how to make the graph's y-axis show values skipping from 25 to 25?
Input Code:
...ANSWER
Answered 2020-Apr-21 at 16:27I think you're looking for scale_y_continuous:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install ggplot2-book
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page