ggplot legend superscript

Hi,

could you please help me with the superscript in the legend?

image

ggplot(data = sf) +
  geom_sf(aes(fill = value), colour = "grey10",size=0.2)+
  scale_fill_gradient2(low = "#376092",
                       mid = "#DCE6F2",
                       high="#CD3C33",
                       midpoint = 10.6,
                       labels= function(x) paste0(format(round(x, 0), nsmall = 0)," tun/km^2"))+
  labs(title = expression(paste("Emise CO"["2"]*"  2017")),
       fill = "") +
  theme(legend.text.align = 1,
        legend.title.align = 0.5,
        legend.text=element_text(size=7),
        plot.title = element_text(hjust = .1,vjust=0.2,size=18),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.text.x=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks=element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())+
  geom_sf_text(aes(label =nazev),size=3,family="sans")+
  annotate("text",x = 14.5, y = 49.8, label = "Středočeský",size=3)+
  annotate("text",x = 17.3, y = 49.6, label = "Olomoucký",size=3)+
  annotate("text",x = 16.7, y = 49.0, label = "Jihomoravský",size=3)+
  ggsave(filename="kraj_emise_CO2_km.png",dpi="print")

Thank you!

Jakub

1 Like

I think when you build the labels for your scale fill manual you have opportunity to use expression like you did for your main title.
I'd play around with it but I don't have your sf so I can't.

1 Like

How can I make sf reproducible?
I thought ^ would make it superscript.

for a reproducible sf, perhaps try rmapshaper::ms_simplify to reduce the geometry as much as possible. it has a keep param which is % of geometry to keep.

If its small enough you might then dput() as normal.

This should do the job.

data.frame(
  stringsAsFactors = FALSE,
        NUTS3_KRAJ = c("CZ010"),
           PLATIOD = c("2001-03-01"),
         NEPLATIPO = c("9999-09-09"),
             value = c(28.5),
             nazev = c("Praha"),
                                 geometry = c("list(c(14.5332841, 14.5324166, 14.5337323, 14.5318076, 14.535351, 14.5399552, 14.5434632, 14.5489568, 14.5541925, 14.5617434, 14.5638667, 14.5616019, 14.5643487, 14.5672752, 14.5699045, 14.5747179, 14.5829601, 14.5854734, 14.5904465, 14.5946018, 14.5991078, 14.594143, 14.5933611, 14.5895526, 14.5888509, 14.5923926, 14.5908553, 14.5930957, 14.5963349, 14.6024274, 14.6097571, 14.6216654, 14.6228963, 14.6333787, 14.6365685, 14.6363276, 14.6465099, 14.6486479, 14.6602449, 14.6607126, 14.6578881, 14.6585641, 
14.6626816, 14.6670129, 14.6730193, 14.6779336, 14.6911147, 14.6898253, 14.6938238, 14.705051, 14.7079204, 14.7037239, 14.7011234, 14.6919909, 14.6832102, 14.6810355, 14.6741501, 14.6682624, 14.6592945, 14.6589988, 14.6525746, 14.6532591, 14.6469498, 14.6413138, 14.6424041, 14.6414676, 14.6448063, 14.6451604, 14.6492463, 14.6549911, 14.6612712, 14.6648547, 14.6679758, 14.6580804, 14.6572497, 14.669157, 14.6706603, 14.6696709, 14.6643149, 14.6627401, 14.6637369, 14.6589058, 14.6509241, 14.6464755, 
14.6440593, 14.6403356, 14.6443248, 14.6480413, 14.6412689, 14.6347177, 14.6302762, 14.6234624, 14.6188923, 14.6149696, 14.609703, 14.6089438, 14.6050134, 14.6033767, 14.5957873, 14.5929499, 14.5902736, 14.588198, 14.5831491, 14.583504, 14.5773892, 14.5727558, 14.5657276, 14.5638599, 14.5554293, 14.5548971, 14.551958, 14.5432095, 14.5420367, 14.5365883, 14.5285754, 14.5235331, 14.5267062, 14.5311217, 14.5254237, 14.5202346, 14.5186163, 14.5104551, 14.5083526, 14.4850287, 14.4856787, 14.4880802, 14.4818482, 
14.4749312, 14.4750853, 14.4705676, 14.4673957, 14.4651991, 14.4633306, 14.4584044, 14.4550753, 14.4510054, 14.4456823, 14.4423515, 14.4313196, 14.4288284, 14.420068, 14.4156776, 14.4126675, 14.4100063, 14.4014403, 14.4002072, 14.3988126, 14.3960318, 14.3954418, 14.3961417, 14.3978563, 14.3966404, 14.3901273, 14.3890486, 14.3808988, 14.3781055, 14.3742835, 14.3730988, 14.3681671, 14.3671665, 14.3614667, 14.3598102, 14.3547419, 14.3476647, 14.3439487, 14.3387051, 14.3360399, 14.3318956, 14.3278379, 
14.3261585, 14.3291919, 14.3357704, 14.3425598, 14.3459891, 14.3472527, 14.3416925, 14.3362419, 14.3333706, 14.3285354, 14.3289297, 14.3336791, 14.3334136, 14.3406638, 14.3386585, 14.3397633, 14.3357305, 14.3368655, 14.3413754, 14.3388137, 14.3360552, 14.3275442, 14.3191719, 14.3157864, 14.3150324, 14.3111732, 14.3082969, 14.3074712, 14.3036274, 14.3008398, 14.2961857, 14.3015998, 14.3012532, 14.3040101, 14.306467, 14.3125293, 14.3127948, 14.3156421, 14.3176096, 14.3166253, 14.3013903, 14.2986641, 
14.290404, 14.2831337, 14.2752104, 14.2729291, 14.2702948, 14.2720703, 14.271373, 14.2737054, 14.2709972, 14.2653754, 14.2633726, 14.2582977, 14.2566643, 14.2519139, 14.2491522, 14.2486255, 14.2543753, 14.2597834, 14.2591214, 14.2678245, 14.2855559, 14.2912658, 14.2904479, 14.2860832, 14.2864074, 14.2827128, 14.2763266, 14.2755499, 14.2693435, 14.2659575, 14.2621656, 14.2621044, 14.256583, 14.2553937, 14.2594159, 14.2509091, 14.2400485, 14.2371099, 14.2338291, 14.2277364, 14.2255466, 14.2265327, 
14.2315692, 14.2373926, 14.2381247, 14.2398755, 14.2471261, 14.2571485, 14.2582336, 14.2611175, 14.2799147, 14.2857397, 14.2889563, 14.2978292, 14.2962033, 14.3012828, 14.3035142, 14.3069968, 14.3110647, 14.3169609, 14.3167783, 14.3187462, 14.3226516, 14.3287263, 14.3404555, 14.3618712, 14.3614915, 14.3567284, 14.3611307, 14.3585312, 14.3572434, 14.3585192, 14.3571785, 14.3611527, 14.3632165, 14.3667945, 14.3726726, 14.3795313, 14.3860078, 14.3922083, 14.3931435, 14.3959957, 14.400242, 14.4011816, 
14.4082166, 14.4136687, 14.422209, 14.421112, 14.4287756, 14.4302813, 14.4292374, 14.4371061, 14.4378159, 14.4529688, 14.4615412, 14.4652284, 14.4649492, 14.4678749, 14.4680192, 14.4812578, 14.4806303, 14.4932971, 14.5080162, 14.5101842, 14.5206948, 14.5277041, 14.5332841, 50.1768804, 50.1732921, 50.1682616, 50.1673761, 50.1623524, 50.164132, 50.1636349, 50.1661652, 50.1645068, 50.1627947, 50.1568132, 50.1561505, 50.1509553, 50.1524341, 50.1508227, 50.1507195, 50.1522114, 50.154428, 50.1550786, 50.1545732, 
50.1549304, 50.1502685, 50.1481329, 50.1479385, 50.1460273, 50.1436638, 50.1404549, 50.1398106, 50.1347037, 50.1299256, 50.1278959, 50.1302162, 50.1286102, 50.1306711, 50.1269248, 50.124698, 50.1241894, 50.1249626, 50.1233828, 50.1214409, 50.1106674, 50.1072776, 50.1059292, 50.1034282, 50.1038849, 50.102421, 50.101458, 50.097075, 50.0962353, 50.0925162, 50.0877922, 50.077322, 50.0729004, 50.07279, 50.0699077, 50.0698691, 50.0676897, 50.0645569, 50.0630559, 50.0620489, 50.0627254, 50.0603097, 50.0595071, 
50.0576664, 50.0538905, 50.0489533, 50.0449378, 50.0429531, 50.044573, 50.0500246, 50.0457278, 50.0416235, 50.0392965, 50.0385864, 50.0317233, 50.0197157, 50.019607, 50.0142003, 50.0143549, 50.0100662, 50.0091683, 50.0051144, 50.0095596, 50.0059215, 50.0068931, 50.00606, 50.0020476, 49.9995192, 49.9952055, 49.9968676, 49.9963023, 49.9993811, 49.9990048, 50.0006223, 50.0008722, 50.0037036, 50.0026924, 50.0101991, 50.0085135, 50.0105276, 50.009425, 50.0122211, 50.0118163, 50.0171325, 50.0143661, 50.0097367, 
50.0103976, 50.0123554, 50.0128599, 50.0106828, 50.0087559, 50.0095498, 50.0106617, 50.012521, 50.0116083, 50.008882, 50.0046439, 50.0014842, 49.9982029, 49.9978689, 49.9959298, 49.9946222, 49.9982462, 49.9931882, 49.9881838, 49.9864553, 49.9859585, 49.9841639, 49.9811716, 49.9808776, 49.9818049, 49.9734584, 49.9715383, 49.9726511, 49.9726616, 49.9741941, 49.9746313, 49.9701056, 49.9676068, 49.9652676, 49.9643514, 49.9661583, 49.9665623, 49.968412, 49.9710968, 49.9685858, 49.9626647, 49.957399, 49.9537654, 
49.9495733, 49.9456264, 49.9426667, 49.9459412, 49.950551, 49.9499626, 49.9474329, 49.9478898, 49.9507306, 49.9520651, 49.9502993, 49.9482226, 49.9494518, 49.9485515, 49.9490143, 49.9483527, 49.9491523, 49.9533359, 49.9558691, 49.9569432, 49.9595568, 49.9649744, 49.9683014, 49.9664997, 49.9683987, 49.9754824, 49.9753858, 49.9736809, 49.9718017, 49.971504, 49.9739738, 49.9766536, 49.9780779, 49.9815244, 49.9831639, 49.9864769, 49.9877096, 49.9896353, 49.9904909, 49.9937951, 49.9946227, 49.992392, 
49.9896671, 49.9935841, 49.995558, 49.9946131, 49.9962435, 49.9982322, 49.9982821, 50.0007677, 50.0030174, 50.0051567, 50.0098138, 50.0119934, 50.0094447, 50.0077548, 50.0099875, 50.0147512, 50.020691, 50.0244467, 50.0254281, 50.0253656, 50.0277471, 50.0326732, 50.0364995, 50.0383696, 50.0435302, 50.0470954, 50.0500312, 50.0513945, 50.0549264, 50.0531162, 50.0563146, 50.0552069, 50.0578716, 50.0575541, 50.0590932, 50.0630091, 50.0633507, 50.0652089, 50.0723027, 50.073414, 50.0743742, 50.0739634, 
50.0779224, 50.079047, 50.0819957, 50.0824052, 50.0818586, 50.0865687, 50.0865343, 50.088047, 50.0878831, 50.0966445, 50.0974906, 50.0992173, 50.1001731, 50.1042594, 50.1028383, 50.1019471, 50.1030261, 50.1012047, 50.1036275, 50.1047932, 50.1058802, 50.1081339, 50.1113008, 50.1125908, 50.1108715, 50.1133297, 50.115355, 50.1142346, 50.119872, 50.11626, 50.1168594, 50.1210479, 50.125444, 50.1274346, 50.1308682, 50.1307424, 50.1288118, 50.1294075, 50.1237358, 50.1195648, 50.1162019, 50.117541, 50.1176869, 
50.1168068, 50.1195347, 50.1254413, 50.1303107, 50.1304371, 50.1374251, 50.1400784, 50.1419634, 50.1448312, 50.1473975, 50.1488235, 50.1487642, 50.1472829, 50.1476968, 50.1447506, 50.1425741, 50.1422185, 50.1441642, 50.1485689, 50.147767, 50.1490193, 50.1501721, 50.1537549, 50.1539364, 50.1547955, 50.1584577, 50.1598815, 50.1583064, 50.1586213, 50.1597829, 50.1607125, 50.1656688, 50.1662164, 50.1703344, 50.1706684, 50.1730846, 50.1731401, 50.1722256, 50.1749504, 50.1756659, 50.1773056, 50.1768804
))",
                       NA)
       
)
#>   NUTS3_KRAJ    PLATIOD  NEPLATIPO value nazev
#> 1      CZ010 2001-03-01 9999-09-09  28.5 Praha
#> 2      CZ010 2001-03-01 9999-09-09  28.5 Praha
#>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               geometry
#> 1 list(c(14.5332841, 14.5324166, 14.5337323, 14.5318076, 14.535351, 14.5399552, 14.5434632, 14.5489568, 14.5541925, 14.5617434, 14.5638667, 14.5616019, 14.5643487, 14.5672752, 14.5699045, 14.5747179, 14.5829601, 14.5854734, 14.5904465, 14.5946018, 14.5991078, 14.594143, 14.5933611, 14.5895526, 14.5888509, 14.5923926, 14.5908553, 14.5930957, 14.5963349, 14.6024274, 14.6097571, 14.6216654, 14.6228963, 14.6333787, 14.6365685, 14.6363276, 14.6465099, 14.6486479, 14.6602449, 14.6607126, 14.6578881, 14.6585641, \n14.6626816, 14.6670129, 14.6730193, 14.6779336, 14.6911147, 14.6898253, 14.6938238, 14.705051, 14.7079204, 14.7037239, 14.7011234, 14.6919909, 14.6832102, 14.6810355, 14.6741501, 14.6682624, 14.6592945, 14.6589988, 14.6525746, 14.6532591, 14.6469498, 14.6413138, 14.6424041, 14.6414676, 14.6448063, 14.6451604, 14.6492463, 14.6549911, 14.6612712, 14.6648547, 14.6679758, 14.6580804, 14.6572497, 14.669157, 14.6706603, 14.6696709, 14.6643149, 14.6627401, 14.6637369, 14.6589058, 14.6509241, 14.6464755, \n14.6440593, 14.6403356, 14.6443248, 14.6480413, 14.6412689, 14.6347177, 14.6302762, 14.6234624, 14.6188923, 14.6149696, 14.609703, 14.6089438, 14.6050134, 14.6033767, 14.5957873, 14.5929499, 14.5902736, 14.588198, 14.5831491, 14.583504, 14.5773892, 14.5727558, 14.5657276, 14.5638599, 14.5554293, 14.5548971, 14.551958, 14.5432095, 14.5420367, 14.5365883, 14.5285754, 14.5235331, 14.5267062, 14.5311217, 14.5254237, 14.5202346, 14.5186163, 14.5104551, 14.5083526, 14.4850287, 14.4856787, 14.4880802, 14.4818482, \n14.4749312, 14.4750853, 14.4705676, 14.4673957, 14.4651991, 14.4633306, 14.4584044, 14.4550753, 14.4510054, 14.4456823, 14.4423515, 14.4313196, 14.4288284, 14.420068, 14.4156776, 14.4126675, 14.4100063, 14.4014403, 14.4002072, 14.3988126, 14.3960318, 14.3954418, 14.3961417, 14.3978563, 14.3966404, 14.3901273, 14.3890486, 14.3808988, 14.3781055, 14.3742835, 14.3730988, 14.3681671, 14.3671665, 14.3614667, 14.3598102, 14.3547419, 14.3476647, 14.3439487, 14.3387051, 14.3360399, 14.3318956, 14.3278379, \n14.3261585, 14.3291919, 14.3357704, 14.3425598, 14.3459891, 14.3472527, 14.3416925, 14.3362419, 14.3333706, 14.3285354, 14.3289297, 14.3336791, 14.3334136, 14.3406638, 14.3386585, 14.3397633, 14.3357305, 14.3368655, 14.3413754, 14.3388137, 14.3360552, 14.3275442, 14.3191719, 14.3157864, 14.3150324, 14.3111732, 14.3082969, 14.3074712, 14.3036274, 14.3008398, 14.2961857, 14.3015998, 14.3012532, 14.3040101, 14.306467, 14.3125293, 14.3127948, 14.3156421, 14.3176096, 14.3166253, 14.3013903, 14.2986641, \n14.290404, 14.2831337, 14.2752104, 14.2729291, 14.2702948, 14.2720703, 14.271373, 14.2737054, 14.2709972, 14.2653754, 14.2633726, 14.2582977, 14.2566643, 14.2519139, 14.2491522, 14.2486255, 14.2543753, 14.2597834, 14.2591214, 14.2678245, 14.2855559, 14.2912658, 14.2904479, 14.2860832, 14.2864074, 14.2827128, 14.2763266, 14.2755499, 14.2693435, 14.2659575, 14.2621656, 14.2621044, 14.256583, 14.2553937, 14.2594159, 14.2509091, 14.2400485, 14.2371099, 14.2338291, 14.2277364, 14.2255466, 14.2265327, \n14.2315692, 14.2373926, 14.2381247, 14.2398755, 14.2471261, 14.2571485, 14.2582336, 14.2611175, 14.2799147, 14.2857397, 14.2889563, 14.2978292, 14.2962033, 14.3012828, 14.3035142, 14.3069968, 14.3110647, 14.3169609, 14.3167783, 14.3187462, 14.3226516, 14.3287263, 14.3404555, 14.3618712, 14.3614915, 14.3567284, 14.3611307, 14.3585312, 14.3572434, 14.3585192, 14.3571785, 14.3611527, 14.3632165, 14.3667945, 14.3726726, 14.3795313, 14.3860078, 14.3922083, 14.3931435, 14.3959957, 14.400242, 14.4011816, \n14.4082166, 14.4136687, 14.422209, 14.421112, 14.4287756, 14.4302813, 14.4292374, 14.4371061, 14.4378159, 14.4529688, 14.4615412, 14.4652284, 14.4649492, 14.4678749, 14.4680192, 14.4812578, 14.4806303, 14.4932971, 14.5080162, 14.5101842, 14.5206948, 14.5277041, 14.5332841, 50.1768804, 50.1732921, 50.1682616, 50.1673761, 50.1623524, 50.164132, 50.1636349, 50.1661652, 50.1645068, 50.1627947, 50.1568132, 50.1561505, 50.1509553, 50.1524341, 50.1508227, 50.1507195, 50.1522114, 50.154428, 50.1550786, 50.1545732, \n50.1549304, 50.1502685, 50.1481329, 50.1479385, 50.1460273, 50.1436638, 50.1404549, 50.1398106, 50.1347037, 50.1299256, 50.1278959, 50.1302162, 50.1286102, 50.1306711, 50.1269248, 50.124698, 50.1241894, 50.1249626, 50.1233828, 50.1214409, 50.1106674, 50.1072776, 50.1059292, 50.1034282, 50.1038849, 50.102421, 50.101458, 50.097075, 50.0962353, 50.0925162, 50.0877922, 50.077322, 50.0729004, 50.07279, 50.0699077, 50.0698691, 50.0676897, 50.0645569, 50.0630559, 50.0620489, 50.0627254, 50.0603097, 50.0595071, \n50.0576664, 50.0538905, 50.0489533, 50.0449378, 50.0429531, 50.044573, 50.0500246, 50.0457278, 50.0416235, 50.0392965, 50.0385864, 50.0317233, 50.0197157, 50.019607, 50.0142003, 50.0143549, 50.0100662, 50.0091683, 50.0051144, 50.0095596, 50.0059215, 50.0068931, 50.00606, 50.0020476, 49.9995192, 49.9952055, 49.9968676, 49.9963023, 49.9993811, 49.9990048, 50.0006223, 50.0008722, 50.0037036, 50.0026924, 50.0101991, 50.0085135, 50.0105276, 50.009425, 50.0122211, 50.0118163, 50.0171325, 50.0143661, 50.0097367, \n50.0103976, 50.0123554, 50.0128599, 50.0106828, 50.0087559, 50.0095498, 50.0106617, 50.012521, 50.0116083, 50.008882, 50.0046439, 50.0014842, 49.9982029, 49.9978689, 49.9959298, 49.9946222, 49.9982462, 49.9931882, 49.9881838, 49.9864553, 49.9859585, 49.9841639, 49.9811716, 49.9808776, 49.9818049, 49.9734584, 49.9715383, 49.9726511, 49.9726616, 49.9741941, 49.9746313, 49.9701056, 49.9676068, 49.9652676, 49.9643514, 49.9661583, 49.9665623, 49.968412, 49.9710968, 49.9685858, 49.9626647, 49.957399, 49.9537654, \n49.9495733, 49.9456264, 49.9426667, 49.9459412, 49.950551, 49.9499626, 49.9474329, 49.9478898, 49.9507306, 49.9520651, 49.9502993, 49.9482226, 49.9494518, 49.9485515, 49.9490143, 49.9483527, 49.9491523, 49.9533359, 49.9558691, 49.9569432, 49.9595568, 49.9649744, 49.9683014, 49.9664997, 49.9683987, 49.9754824, 49.9753858, 49.9736809, 49.9718017, 49.971504, 49.9739738, 49.9766536, 49.9780779, 49.9815244, 49.9831639, 49.9864769, 49.9877096, 49.9896353, 49.9904909, 49.9937951, 49.9946227, 49.992392, \n49.9896671, 49.9935841, 49.995558, 49.9946131, 49.9962435, 49.9982322, 49.9982821, 50.0007677, 50.0030174, 50.0051567, 50.0098138, 50.0119934, 50.0094447, 50.0077548, 50.0099875, 50.0147512, 50.020691, 50.0244467, 50.0254281, 50.0253656, 50.0277471, 50.0326732, 50.0364995, 50.0383696, 50.0435302, 50.0470954, 50.0500312, 50.0513945, 50.0549264, 50.0531162, 50.0563146, 50.0552069, 50.0578716, 50.0575541, 50.0590932, 50.0630091, 50.0633507, 50.0652089, 50.0723027, 50.073414, 50.0743742, 50.0739634, \n50.0779224, 50.079047, 50.0819957, 50.0824052, 50.0818586, 50.0865687, 50.0865343, 50.088047, 50.0878831, 50.0966445, 50.0974906, 50.0992173, 50.1001731, 50.1042594, 50.1028383, 50.1019471, 50.1030261, 50.1012047, 50.1036275, 50.1047932, 50.1058802, 50.1081339, 50.1113008, 50.1125908, 50.1108715, 50.1133297, 50.115355, 50.1142346, 50.119872, 50.11626, 50.1168594, 50.1210479, 50.125444, 50.1274346, 50.1308682, 50.1307424, 50.1288118, 50.1294075, 50.1237358, 50.1195648, 50.1162019, 50.117541, 50.1176869, \n50.1168068, 50.1195347, 50.1254413, 50.1303107, 50.1304371, 50.1374251, 50.1400784, 50.1419634, 50.1448312, 50.1473975, 50.1488235, 50.1487642, 50.1472829, 50.1476968, 50.1447506, 50.1425741, 50.1422185, 50.1441642, 50.1485689, 50.147767, 50.1490193, 50.1501721, 50.1537549, 50.1539364, 50.1547955, 50.1584577, 50.1598815, 50.1583064, 50.1586213, 50.1597829, 50.1607125, 50.1656688, 50.1662164, 50.1703344, 50.1706684, 50.1730846, 50.1731401, 50.1722256, 50.1749504, 50.1756659, 50.1773056, 50.1768804\n))
#> 2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 <NA>

Created on 2020-08-04 by the reprex package (v0.3.0)

Using expression seems a good way to go, but you have trouble getting a vector (since expression won't evaluate format, so will dump the input vector all at once in a single expression). A solution is to use parse(text = ...) or equivalently str2expression() to execute format, make a string with paste, then turn it into an expression.

One caveat is that the expression has to be valid, so you need an operator (e.g. * or ~) between the number and the unit. This works on a different example:

labels = function(x) str2expression(paste0(format(round(x, 0), nsmall = 0), " ~ tun/km^2"))
3 Likes

Hi, thank you! I cannot find any function str2expression().

typo ? str2expression

Sorry, I meant str2expression().

Error in str2expression(paste0(format(round(x, 0), nsmall = 0), " ~ tun/km^2")) :
could not find function "str2expression"

not even in R Documentation

str2expression joined base R in version 3.6.0

2 Likes

This should work the same:

labels= function(x) parse(text = paste0(format(round(x, 0), nsmall = 0), " ~ tun/km^2")))

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.