tutorial tamaño poner leyenda ggplot español como change r ggplot2

r - tamaño - leyenda en ggplot2



Modificar fuentes en ggplot2 (7)

Estoy buscando una manera de modificar los tipos de fuente en ggplot. Por el momento, me alegraría simplemente cambiar las fuentes a la familia de fuentes ''courier'', pero en última instancia mi objetivo es llamar a una plantilla de fuente personalizada; cualquier comentario sobre este último punto sería muy apreciado.

He hecho un poco de tarea, mirando las siguientes publicaciones y artículos:

Puede ser porque todavía soy un aficionado sin esperanza con ggplot2, pero ni siquiera he podido cambiar las fuentes de gráficos a mensajería. ¿Alguna ayuda? He incluido los datos para la tabla en cuestión, a continuación, junto con el código, así que espero que esto sea lo suficientemente fácil de seguir.


Creo que tu respuesta está bien, pero puedes hacerlo de manera más simple:

install.packages("extrafont");library(extrafont) font_import("Trebuchet MS") library(ggplot2) qplot(1:10)+theme(text=element_text(family="Trebuchet MS"))


Eche un vistazo al argumento familiar de theme_text ()

dummy <- data.frame(A = rnorm(10), B = rnorm(10)) ggplot(dummy, aes(x = A, y = B)) + geom_point() #helvetica = default ggplot(dummy, aes(x = A, y = B)) + geom_point() + opts(axis.title.x = theme_text(family = "sans", face = "bold")) #times ggplot(dummy, aes(x = A, y = B)) + geom_point() + opts(axis.title.x = theme_text(family = "serif", face = "bold")) #courier ggplot(dummy, aes(x = A, y = B)) + geom_point() + opts(axis.title.x = theme_text(family = "mono", face = "bold"))


Esta parece ser la solución más simple, por mi dinero.

Algunos reproducen datos en df y se convierten en un gráfico simple, "p", con buenas etiquetas largas xey, para que podamos ver el cambio de fuente:

df <- data.frame(A = rnorm(10), B = rnorm(10)) p = ggplot(data = df, aes(x = A, y = B)) + geom_point() p = p + xlab("A long x-string so we can see the effect of the font switch") p = p + ylab("Likewise up the ordinate")

Y vemos el diagrama predeterminado en cualquier fuente que sea:

p

Ahora cambiamos a Optima, agregando un buen título y subtítulo para disfrutar de la gloria de Optima:

label = "Now we switch to Optima" subtitle = "Optima is a nice font: https://en.wikipedia.org/wiki/Optima#Usages"

Y después de todo eso, imprimimos en la nueva fuente

# the only line you need to read: p + theme(text = element_text(family = "Optima", , face = "bold")) p = p + ggtitle(label = label, subtitle = subtitle) p


Inspirado por una publicación en el blog de kohske , se me ocurrió esto:

theme_set( theme_bw( base_family= "serif")) theme_update( panel.grid.minor= theme_blank(), panel.grid.major= theme_blank(), panel.background= theme_blank(), axis.title.x= theme_blank(), axis.text.x= theme_text( family= "serif", angle= 90, hjust= 1 ), axis.text.x= theme_text( family= "serif"), axis.title.y= theme_blank()) theme_map <- theme_get() theme_set( theme_bw())

Ahora cuando quiero usar ese tema en particular:

last_plot() + theme_map

YMMV.

Por cierto, si tuviera el poder votaría por la respuesta preferida:

> grid.gedit("GRID.text",gp=gpar(fontfamily="mono")) Error in editDLfromGPath(gPath, specs, strict, grep, global, redraw) : ''gPath'' (GRID.text) not found

No estoy seguro que significa esto. Tampoco me ofrecieron un enlace para comentar sobre esa respuesta; tal vez algo ha cambiado en el sitio.


Puede establecer la fuente de las etiquetas producidas por geom_text con grid.gedit :

grid.gedit("GRID.text",gp=gpar(fontfamily="mono"))

Llámalo después de haber producido tu trama original.


Resolvió mi consulta con una molestia bastante mínima. Fue una solución de dos pasos a la que no habría llegado sin seguir los consejos de los miembros que respondieron.

Para cambiar los valores predeterminados del texto ggplot, adapté el código al que Brandon me remitió en:

http://johndunavent.com/combined-line-and-bar-chart-ggplot2

Donde John Dunavent crea una función, theme_min, que se puede editar para proporcionar las opciones predeterminadas para un ggplot, incluido el uso de fuentes importadas de Windows con el comando windowsFonts. Mi adaptación de su código se ve así:

theme_min = function (size=10, font=NA, face=''plain'', panelColor=backgroundColor, axisColor=''#999999'', gridColor=gridLinesColor, textColor=''black'') { theme_text = function(...) ggplot2::theme_text(family=font, face=face, colour=textColor, size=size, ...) opts( axis.text.x = theme_text(), axis.text.y = theme_text(), axis.line = theme_blank(), axis.ticks = theme_segment(colour=axisColor, size=0.25), panel.border = theme_rect(colour=backgroundColor), legend.background = theme_blank(), legend.key = theme_blank(), legend.key.size = unit(1.5, ''lines''), legend.text = theme_text(hjust=0), legend.title = theme_text(hjust=0), panel.background = theme_rect(fill=panelColor, colour=NA), panel.grid.major = theme_line(colour=gridColor, size=0.33), panel.grid.minor = theme_blank(), strip.background = theme_rect(fill=NA, colour=NA), strip.text.x = theme_text(hjust=0), strip.text.y = theme_text(angle=-90), plot.title = theme_text(hjust=0), plot.margin = unit(c(0.1, 0.1, 0.1, 0.1), ''lines'')) } ##Create a custom font type. Could be ''F'', ''TEST'', whatever windowsFonts(F = windowsFont(''Wide Latin'')) ##and insert this line of code into the original code I list above: + theme_min(font=''F'', size=10)

Torpemente, no hay manera (que encontré) de modificar genéricamente la configuración de la fuente para los objetos geom_text antes de que se cree una trama. La solución anterior de James funcionó perfectamente para esto, sin embargo. En lugar de usar una fuente estándar, configuré fontfamily = "F" para traer la fuente personalizada que seleccioné en theme_min (), es decir:

grid.gedit("GRID.text",gp=gpar(fontfamily="F"))

Es de esperar que esto sea útil para cualquier otro usuario que desee modificar las fuentes en sus gráficos.

¡Saludos a todos los que me ayudaron a resolver esto! Aaron