Coder Social home page Coder Social logo

Comments (6)

wilkox avatar wilkox commented on June 9, 2024

(Link to the previous related issue)

First example

What do setting teh ymin and ymax asthetics do? As I understand it they are the y limits on the box that the text is placed into. ymin being a value on the plot I understand, but ymax =1 I don't?

That's correct: ymin and ymax set the y limits of the box. In the example from the previous issue, we want the box to extend from the top of the column to the top of the plot area, so ymax was set to a value that allows enough 'headroom' over each bar to draw the text. Other values would also work:

library(ggplot2)
library(ggfittext)

ggplot(coffees, aes(x = coffee, y = proportion, label = ingredient, fill = ingredient)) + 
  geom_col(position = "dodge") + 
  geom_fit_text(aes(ymin = proportion, ymax = 0.6), position = "dodge", place = "bottom", grow = TRUE, reflow = TRUE)

Created on 2019-06-27 by the reprex package (v0.3.0)

geom_bar_text() puts the text inside the bar so that you don't have to do this manual fiddling to set the 'headroom'.

Second example

The labels are different sizes and some are missing (presumably so small they could not be displayed.

I don't understand why this is happening if all the text is so similar?

library(ggfittext)
library(tidyverse)

pdat <- tribble(
  ~key, ~value, ~Name, ~value_label,
  "key", 2.02942183, 1, 2.03,
  "key", 1.27338181, 2, 1.27,
  "key", 4.98740337, 3, 4.99,
  "key", 1.43705013, 4, 1.44,
  "key", 0.07890787, 5, 0.08,
  "key", 2.34419584, 6, 2.34,
  "key", 1.66281582, 7, 1.66,
  "key", 2.28072594, 8, 2.28,
  "key", 7.88383825, 9, 7.88,
  "key", 1.40415457, 10, 1.40,
  "key", 5.54193876, 11, 5.54,
  "key", 0.16704932, 12, 0.17,
  "key", 11.75658642, 13, 11.76,
  "key", 1.13801370, 14, 1.14,
  "key", 1.86561319, 15, 1.87,
  "key", 0.77266434, 16, 0.77,
  "key", 1.29502135, 17, 1.30,
  "key", 1.56975971, 18, 1.57,
  "key", 0.31144624, 19, 0.31,
  "key", 2.38041735, 20, 2.38,
  "key", 0.95595724, 21, 0.96
)

ggplot(pdat, aes(x = Name, y = value, fill = key, label = value_label)) +
  geom_col() +
  geom_fit_text(
    aes(ymin = value_label, ymax = 13),
    place = "bottom",
    grow = FALSE,
    reflow = TRUE
  )

Created on 2019-06-27 by the reprex package (v0.3.0)You are correct that some of the labels are missing because they are too small to display. This threshold can be set with the min.size option – any text shrunk smaller than min.size (expressed in pts) will be hidden. If min.size is set to 0, no text will be hidden.

On the question of why some labels are different sizes, the answer is that some of them are actually much wider than others so need to be shrunk down. For example, let's compare the values on column 10 and column 13 as they are drawn by grid (the drawing layer that underpins ggplot2):

library(grid)
grid.newpage()
grid.text(label = "1.4", y = 0.6, gp = gpar(fontsize = 48))
grid.text(label = "11.76", y = 0.5, gp = gpar(fontsize = 48))

Created on 2019-06-27 by the reprex package (v0.3.0)

"11.76" is about twice the width of "1.4", which is why it needs to be shrunk to a much smaller font size to fit the width of the column.

from ggfittext.

wdm0336 avatar wdm0336 commented on June 9, 2024

OK. So I understand the intention.

I ran one more test that produced the attached plot. I changed the value_label column to contain the same value for each row. I would think the labels would all look the same since the columns should be the same width. The attached plot shows otherwise. I am wondering if there is something else that is influencing the font chosen?

Thanks for putting this package together!

Bill

Rplot03

from ggfittext.

wilkox avatar wilkox commented on June 9, 2024

Could you post the code that produced the plot? Looking at the plot I suspect ymax is being set too low – it might be better if you used a value like 13 or 14.

from ggfittext.

wdm0336 avatar wdm0336 commented on June 9, 2024

Here is the code:

`
pdat <- tribble(
~key, ~value, ~Name, ~value_label,
"key", 2.02942183, 1, 2.03,
"key", 1.27338181, 2, 1.27,
"key", 4.98740337, 3, 4.99,
"key", 1.43705013, 4, 1.44,
"key", 0.07890787, 5, 0.08,
"key", 2.34419584, 6, 2.34,
"key", 1.66281582, 7, 1.66,
"key", 2.28072594, 8, 2.28,
"key", 7.88383825, 9, 7.88,
"key", 1.40415457, 10, 1.40,
"key", 5.54193876, 11, 5.54,
"key", 0.16704932, 12, 0.17,
"key", 11.75658642, 13, 11.76,
"key", 1.13801370, 14, 1.14,
"key", 1.86561319, 15, 1.87,
"key", 0.77266434, 16, 0.77,
"key", 1.29502135, 17, 1.30,
"key", 1.56975971, 18, 1.57,
"key", 0.31144624, 19, 0.31,
"key", 2.38041735, 20, 2.38,
"key", 0.95595724, 21, 0.96
)

pdat$value_label = 0.31
pdat$value_label <- as.character(pdat$value_label)

ggplot(pdat, aes(x = Name, y = value, fill = key, label = value_label)) +
geom_col() +
ggfittext::geom_fit_text(aes(ymin = value, ymax = 1),
place = "bottom",
grow = FALSE,
reflow = TRUE)
`

from ggfittext.

wdm0336 avatar wdm0336 commented on June 9, 2024

When I change the ggplot call above, I can get it to work. ymax was the issue:

`
ggplot(pdat, aes(x = Name, y = value, fill = key, label = value_label)) +
geom_col() +
ggfittext::geom_fit_text(aes(ymin = value, ymax = max(value) * 1.1),
place = "bottom",
grow = FALSE,
reflow = TRUE)

`

from ggfittext.

wilkox avatar wilkox commented on June 9, 2024

Glad to hear it. Let me know if you hit any more issues.

from ggfittext.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.