ggtext ใช้ภาษาจัดรูปแบบใดในการจัดรูปแบบข้อความ
ฉันพยายามแสดงสัญกรณ์วิทยาศาสตร์บนแกน ggplot2 เป็นตัวหนาโดยใช้รูปแบบ "Ax10 ^ B" ตามตัวอักษรไม่ใช่รูปแบบ "AeB" ซึ่งเป็นค่าเริ่มต้นของ ggplot2 เมื่อรหัสนี้ถูกเรียกใช้
library(tidyverse)
library(ggtext)
ggplot(mpg, aes(displ, hwy*10^9)) + geom_point()
#makes the scientific notation using "AeB" explicitly write out Ax10^B
fancy_scientific <- function(l) {
# turn in to character string in scientific notation
l <- format(l, scientific = TRUE)
# quote the part before the exponent to keep all the digits
l <- gsub("^(.*)e", "'\\1'e", l)
# turn the 'e+' into plotmath format
l <- gsub("e", "%*%10^", l)
# return this as an expression
parse(text=l)
}
ggplot(mpg, aes(displ, hwy*10^9)) +
theme_classic() +
geom_point() +
scale_y_continuous(labels= fancy_scientific) +
theme(text = element_text(face = "bold"),
axis.text.y = element_markdown(face = "bold"))
นี่คือผลลัพธ์:

ฉันใช้element_markdown()
จากggtext
เนื่องจากอนุญาตให้ถ่ายโอนใบหน้าที่เป็นตัวหนาตามที่ฉันค้นพบที่นี่: ฉันจะสร้างรูปแบบข้อความที่กำหนดเองของ ggplot2 จากฟังก์ชันมาตราส่วนแกนตามข้อกำหนดรูปแบบที่ตั้งไว้ในธีม () ได้อย่างไร
ฉันสามารถแก้ไขเครื่องหมายคำพูดคู่ได้โดยเปลี่ยน'\\1'
เป็น\\1
(ลบเครื่องหมายคำพูดเดี่ยว) แต่ฉันมีปัญหาในการแสดงเครื่องหมายคูณ ฉันสามารถใช้ตัวพิมพ์เล็กx
แต่ขี้เกียจ
พอลองใช้$\times$
ตามที่แนะนำนี้https://rstudio-pubs-static.s3.amazonaws.com/18858_0c289c260a574ea08c0f10b944abc883.htmlฉันได้รับข้อผิดพลาด บทความสั้นสำหรับggtext
ดูเหมือนว่าจะใช้ html:https://cran.r-project.org/web/packages/ggtext/vignettes/theme_elements.htmlแต่พวกเขาใช้<sup>
แท็กซึ่งดูเหมือนจะขัดกับการใช้^
เพื่อสร้างเลขชี้กำลังที่นี่และแท็กไม่ทำงานเมื่อฉันใช้แท็กและทรัพยากรทั้งหมดสำหรับ "เครื่องหมายการคูณใน html" ที่ฉันค้นหาไม่ได้ให้คำตอบ . ดังนั้นคำถามของฉันคือฉันจะหาแหล่งข้อมูลที่ดีในการเรียนรู้ภาษาการจัดรูปแบบที่เหมาะสมที่ggtext
/ ggplot2
ใช้สำหรับป้ายกำกับแกนได้ที่ไหน ยังต้องการทราบวิธีแก้ปัญหาเฉพาะที่ฉันมี
คำตอบ
{ggtext} ใช้ Markdown / HTML คุณสามารถแทรกอักขระพิเศษได้โดยใช้อักขระ Unicode หรือโดยใช้เอนทิตี HTML ×
ที่นี่คุณอาจต้องการ
นอกจากนี้อย่าแยกวิเคราะห์สตริงเป็นนิพจน์เมื่อทำงานกับ {ggtext}
library(tidyverse)
library(ggtext)
#makes the scientific notation using "AeB" explicitly write out Ax10^B
fancy_scientific <- function(l) {
# turn in to character string in scientific notation
l <- format(l, scientific = TRUE)
# quote the part before the exponent to keep all the digits
l <- gsub("^(.*)e", "\\1e", l)
# turn the 'e+' into plotmath format
l <- gsub("e", "×10^", l)
# return this as a string
l
}
ggplot(mpg, aes(displ, hwy*10^9)) +
theme_classic() +
geom_point() +
scale_y_continuous(labels= fancy_scientific) +
theme(text = element_text(face = "bold"),
axis.text.y = element_markdown(face = "bold"))
สร้างเมื่อ 2020-08-20 โดยแพ็คเกจ reprex (v0.3.0)
นี่คือเวอร์ชันที่มีเพียงplotmath
นิพจน์:
library(dplyr)
library(ggplot2)
fancy_scientific <- function(l) {
l <- format(l, scientific = TRUE)
parse(text=gsub("(.*)e(\\+?)(\\-?[0-9]+)",
"bold('\\1') * bold(' * ') * bold('10')^bold('\\3')", l))
}
mpg %>% dplyr::mutate(hwy = hwy * 1e9) %>%
ggplot(aes(displ, hwy)) +
theme_classic() +
geom_point() +
scale_y_continuous(labels= fancy_scientific) +
theme(text = element_text(face = "bold"))
... และนี่คือเวอร์ชันที่มีggtext
:
library(dplyr)
library(ggplot2)
library(ggtext)
fancy_scientific <- function(l) {
l <- format(l, scientific = TRUE)
parse(text=gsub("(.*)e(\\+?)(\\-?[0-9]+)", "\\1 * 10^(\\3)", l))
## this would also work, instead of the line above:
# gsub("(.*)e(\\+?)(\\-?[0-9]+)", "**\\1 \\* 10<sup>\\3</sup>**", l)
}
mpg %>% dplyr::mutate(hwy = hwy * 1e9) %>%
ggplot(aes(displ, hwy)) +
theme_classic() +
geom_point() +
scale_y_continuous(labels= fancy_scientific) +
theme(text = element_text(face = "bold"),
axis.text.y = element_markdown(face = "bold"))
สร้างเมื่อ 2020-08-20 โดยแพ็คเกจ reprex (v0.3.0)