Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Figure label auto-positioning #4771

Open
apberesford opened this issue Sep 23, 2024 · 2 comments
Open

Figure label auto-positioning #4771

apberesford opened this issue Sep 23, 2024 · 2 comments
Labels
cs customer success feature something new

Comments

@apberesford
Copy link

This is a customer requested feature. In both R and MatPlotLib there are extensions which allow figure labels to auto-position for readability by preventing overlap. See gif below.

GIF Recording 2024-09-19 at 9 31 39 AM (1)

It is possible to acheive this effect in MatPlotLib and R

At least one customer has requested, and it's a feature which is not possible for a client to easily implement by themeselves. Current label positioning options are not dynamic in this way, and as a result we don't see a lot of client figures using labels, especially when there are more than a few points on the figure. Most clients default to hover, but that isn't viable for everyone, especially in settings where apps are being viewed on different devices. It's potentially also an accessibility improvement.

@apberesford apberesford added feature something new cs customer success labels Sep 23, 2024
@sOnU1002
Copy link

sOnU1002 commented Oct 2, 2024

can u assign the issue to me sir please

@sOnU1002
Copy link

sOnU1002 commented Oct 2, 2024

Feature: Dynamic Label Positioning for Readability

Description:

This feature introduces dynamic label positioning to prevent overlap in scatter plots, ensuring labels are readable without manual adjustment. This improves the accessibility and usability of plots, especially in environments where hover interactions are not feasible (e.g., mobile devices).

Changes:

  • Matplotlib (Python): Added support for automatic label adjustment using the AdjustText package.
  • ggplot2 (R): Implemented ggrepel to dynamically reposition labels for better readability in complex plots.

Reasoning:

Client requests for a feature that improves the readability of labels in scatter plots, especially in dense visualizations. Static label placement often results in overlapping text, which reduces the clarity of figures and discourages clients from using labels. This update provides an automatic solution, improving both accessibility and device compatibility.

Code Implementation:

Python (Matplotlib + AdjustText)
import matplotlib.pyplot as plt
from adjustText import adjust_text

# Sample data
cars = ["Toyota Corolla", "Fiat 128", "Honda Civic", "Lotus Europa", "Fiat X1-9", "Porsche 914-2", ...]
wt = [2.875, 2.32, 1.615, 2.2, 1.835, 2.5, ...]  # weights
mpg = [33.9, 32.4, 33.9, 30.4, 31.9, 26.0, ...]  # mpg values

fig, ax = plt.subplots()
ax.scatter(wt, mpg)

# Adding labels
texts = [ax.text(wt[i], mpg[i], cars[i], fontsize=8) for i in range(len(cars))]

# Adjust text to avoid overlap
adjust_text(texts, arrowprops=dict(arrowstyle="-", color='grey'))

plt.xlabel('Weight')
plt.ylabel('Miles per Gallon')
plt.show()
R (ggplot2 + ggrepel)
r
Copy code
library(ggplot2)
library(ggrepel)

# Using mtcars dataset as an example
mtcars$car <- rownames(mtcars)

ggplot(mtcars, aes(x = wt, y = mpg, label = car)) +
  geom_point() +
  geom_text_repel() +
  labs(x = "Weight", y = "Miles per Gallon") +
  theme_minimal()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cs customer success feature something new
Projects
None yet
Development

No branches or pull requests

2 participants