I have the following dataset
structure(list(X = c(9.8186734, 19.6373468, 29.4560202, 39.2746936,
49.093367, 58.9120404, 68.7307138, 78.5493872, 88.3680606, 98.186734
), Y = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), radii = c(530.595715856625,
530.595715856625, 524.270569515141, 520.785212389348, 524.423046929159,
524.777454042683, 523.089321742221, 522.852371975715, 523.124870390148,
522.612174462367), slope = c(-21.796356958782, -21.796356958782,
-21.796356958782, -21.796356958782, -21.796356958782, -21.796356958782,
-21.796356958782, -21.796356958782, -21.796356958782, -21.796356958782
)), row.names = c(NA, -10L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x7f989f011ce0>, sorted = "Y")
and I am simply trying to print slope as a text to the figure as
str_slope <- c(substitute("Slope = "~sp~mu*"m/s", list(sp = sprintf("%2.1f", dt[!duplicated(slope), slope]))))
d_text <- data.table(x=2000, y=500, label = str_slope)
ggplot(dt, aes(x = X, y=radii)) +
geom_point()+
geom_smooth(method = "lm", level = 0.9999, se = TRUE)+
scale_colour_manual(values = getPalette) +
labs(x = "time (s)", y = expression("radius ["*mu*"m]"), color = "Speed [µm/s]") +
geom_text(data = d_text, aes(x = x, y = y, label = label))+
theme_default(legend.position = "none")
but I get something like this
Why is the text in str_slope not evaluated as an expression? How can I force ggplot to interpret it as an expression, so that the text will look like
For this type of plot annotation, you should use annotate(geom="text"...) rather than geom_text(). For how to generate the expression, you can use the parse=TRUE argument within annotate().
I think we're missing all your plot data, so here's an example with mtcars that incorporates the contents of str_slope in your example.
str_slope <- c(substitute("Slope = "~sp~mu*"m/s", list(sp = sprintf("%2.1f", dt[!duplicated(slope), slope]))))
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p + annotate(
"text", x=4, y=25,
label= str_slope, parse=TRUE)
For your information, geom_text() is designed to be used wherein one or more aesthetics are mapped to a data frame. If you have only one line of text you want to appear on the plot, you should use annotate(geom="text"...), which is used when you do not want to map any aesthetics to a data frame.
Related
I have this dataset:
structure(list(new = c("No: 0.91", "Yes: 0.89", "All: 0.84")), row.names = c(NA,
-3L), class = c("tbl_df", "tbl", "data.frame"))
And I would like to create a plot with only the text like:
But the text to be one below the other. So you have any idea?
One option would be to use nudge_y to shift the labels:
library(ggplot2)
ggplot() +
geom_text(aes(
x = 1, y = 1,
label = c("No: 0.91", "Yes: 0.89", "All: 0.84")
), nudge_y = .05 * c(1, 0, -1))
An alternative is to specify the y axis positions manually within aes
ggplot(dataset, aes(0, -seq_along(new) + 2)) +
geom_text(aes(label = new, color = new)) +
ylim(c(-2, 2)) +
scale_color_brewer(palette = 'Set1', guide = 'none')
Given this simple data I want to plot the equivalent of base
plot(dat$value)
but with ggplot.
dat= structure(list(name = c("Cord", "Cord",
"amo", "amo",
"amo", "ramo"),
value = c(7, 0.7, 9,
0.9, 0.8, 0.7)), row.names = c(NA,
6L), class = "data.frame")
I tried:
> ggplot(data = dat) + geom_point(aes(x = value, colour = name))
Error in `check_required_aesthetics()`:
! geom_point requires the following missing aesthetics: y
I need to plot "count" on y axis vs value on x axis
You could create a row index using tibble::rownames_to_column, then use that to plot along the x-axis, so that you get a similar result to plot(dat$value).
library(tidyverse)
dat %>%
rownames_to_column("ind") %>%
ggplot(aes(x = ind, y = value, color = name)) +
geom_point(size = 3) +
theme_bw()
Output
Or you can put the function directly into ggplot:
ggplot(dat, aes(
x = rownames_to_column(dat)$rowname,
y = value,
color = name
)) +
geom_point(size = 3) +
theme_bw()
Or another option is to use row.names:
ggplot(dat, aes(x = as.numeric(row.names(dat)), y = value, colour = name)) +
geom_point()
Base R plot(dat$value) has an implicit x equal to seq_along(dat$value).
Use x = seq_along(value) and y = value.
dat <- structure(list(name = c("Cord", "Cord",
"amo", "amo",
"amo", "ramo"),
value = c(7, 0.7, 9,
0.9, 0.8, 0.7)),
row.names = c(NA, 6L),
class = "data.frame")
library(ggplot2)
ggplot(dat, aes(x = seq_along(value), y = value, color = name)) +
geom_point()
Created on 2022-03-01 by the reprex package (v2.0.1)
I have multiple time-series plots. An example plot and code can be found below. I construct the plot using ggplot2 and make it interactive using ggplotly().
However, date format on the smoothed curve get lost. Interactive chart shows date as some numeric values.
How can I fix the problem?
Thank you very much
structure(list(Date = structure(c(15736, 15764, 15795, 15825,
15856, 15886), class = "Date"), CLI = c(99.93, 100.3, 100.96,
100.71, 100.62, 101.15)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
plot5 <- df %>%
ggplot(aes(x = Date, y = CLI))+
geom_line(size = 0.5, alpha = 0.75, show.legend = FALSE, color = "steelblue4")+
scale_x_date(date_breaks = "6 month", date_labels = "%m/%y")+
theme_pander()+
geom_line(stat='smooth', method = "glm", alpha=0.5, color = "firebrick2", formula = y ~ poly(x, 5))+
geom_ribbon(stat='smooth',method = "glm", se=TRUE,formula = y ~ poly(x, 5), alpha=0.01)+
labs(x = "Date",
y = "Composite Leading Indicator")
ggplotly(plot5)
Adapting my answer on this post to your case one option to get the date format in the tooltip would be to make use of the text aesthetic to manually create the tooltip and convert the numbers to proper dates like so:
plot <- df %>%
ggplot(aes(x = Date, y = CLI)) +
geom_line(size = 0.5, alpha = 0.75, show.legend = FALSE, color = "steelblue4") +
scale_x_date(date_labels = "%m/%y") +
# theme_pander()+
geom_line(aes(text = paste(
"date: ", as.Date(..x.., origin = "1970-01-01"), "<br>",
"y:", ..y..
)), stat = "smooth", method = "glm", alpha = 0.5, color = "firebrick2", formula = y ~ poly(x, 5)) +
geom_ribbon(stat = "smooth", method = "glm", se = TRUE, formula = y ~ poly(x, 5), alpha = 0.01) +
labs(
x = "Date",
y = "Composite Leading Indicator"
)
ggplotly(plot, tooltip = c("text"))
I am trying to replicate the bar plot as shown bellow.
Here is an example of the data frame. Where the y variable is tasa and the x variable is year, and the number showed in the text of each x tick label is inscripciones.
df <- structure(list(year = c("2018", "2019"), inscripciones = c(3038910, 3680696), tasa = c(88.9528707645112, 104.692208214133)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -2L))
p <- ggplot(data = df, aes(x = year, y = tasa)) +
geom_bar(width = 0.4, stat = "identity", fill="orange")+
geom_text(aes(year, tasa + 5, label = round(tasa,2), fill = NULL), size=4)+
labs(x = NULL, y = NULL)+
scale_y_continuous(breaks = seq(0, 110, by = 10))+
theme_bw()
How can I add these long text including information from the dataframe to the x tick labels?
Firstly, your data & plot combination are not reproducible. I renamed annoh as year then create the plot p.
Then,scale_x_discrete with "\n" strings works when you want to skip lines;
long_text_1 <- 'Gün, senden ışık alsa\n da bir renge bürünse;\n
Ay, secde edip çehrene,\n yerlerde sürünse;\n
Her şey silinip\n kayboluyorken nazarımdan,\n
Yalnız o yeşil\n gözlerinin nuru görünse...'
long_text_2 <- 'Ruhun mu ateş,\nyoksa o gözler mi alevden?\n
Bilmem bu yanardağ\n ne biçim korla tutuştu?\n
Pervane olan kendini\n gizler mi hiç alevden?\n
Sen istedin ondan bu\n gönül zorla tutuştu.'
p <- ggplot(data = df, aes(x = year, y = tasa)) +
geom_bar(width = 0.4, stat = "identity", fill="orange")+
geom_text(aes(year, tasa + 5, label = round(tasa,2), fill = NULL), size=4)+
labs(x = NULL, y = NULL)+
scale_y_continuous(breaks = seq(0, 110, by = 10))+
theme_bw()+
scale_x_discrete(labels=c('2018'=long_text_1,'2019'=long_text_2))
I am using ggplot to make a bar chart. I have already used the scales package to change the scientific "2e5" formatting to the full number with commas to separate. I have been unable to change the axis tick labels so that a value of 1,000,000 appears as 1M, and 3,500,000 as 3.5M, etc. How do I do this?
See below for code:
# Generate dummy dataframe
df <- structure(list(month = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
), foo = c(2322636.14889234, 8676432.48522654, 207993.984222412,
3310791.19816422, 7540729.19022292, 7316447.75252789, 2410026.6979076,
6202864.60500211, 8700672.56037146, 1334956.53280988, 505991.168320179,
3106733.97500068)), row.names = c(NA, -12L), class = "data.frame")
# create plot
plot.1 <- ggplot2::ggplot(data = df, aes(x = month, y = foo)) +
geom_bar(stat = 'identity', fill = 'darkorchid4', width = 0.5) +
theme_minimal() +
labs(title = "Monthly foo measurements", x = "Month",
y = "Amount of foo" ) +
scale_y_continuous(labels = scales::comma)
Thanks in advance!
ggplot2::ggplot(data = df, aes(x = month, y = foo)) +
geom_bar(stat = 'identity', fill = 'darkorchid4', width = 0.5) +
theme_minimal() +
labs(title = "Monthly foo measurements", x = "Month",
y = "Amount of foo" ) +
scale_y_continuous(label = scales::unit_format(unit = "M", scale = 1e-6, sep = ""))