{"id":1715,"date":"2024-08-30T14:04:51","date_gmt":"2024-08-30T12:04:51","guid":{"rendered":"https:\/\/inseri.swiss\/?p=1715"},"modified":"2024-11-12T14:04:51","modified_gmt":"2024-11-12T14:04:51","slug":"r-and-plotting","status":"publish","type":"post","link":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/2024\/08\/r-and-plotting\/","title":{"rendered":"R and Plotting"},"content":{"rendered":"\n<p>In this example we will show you how to integrate our R code for generating plots into the R Code block and use the Image Box block to display it.<\/p>\n\n\n\n<p>For this use case please pay attention that we save the image as a PNG file.<\/p>\n\n\n\n<p>The PNG file is the <strong>output<\/strong> of our R Code block, and used further as an input in the Image Box block.<\/p>\n\n\n\n<div class=\"wp-block-inseri-core-r-code\" data-attributes=\"{&quot;autoTrigger&quot;:false,&quot;blockId&quot;:&quot;GYRJtLeZ3gBH9u1ud4H1a&quot;,&quot;content&quot;:&quot;png(file%3D%22barplot.png%22)%0Abarplot(c(1%2C%203%2C%202)%2C%20name.arg%20%3D%20c(%22a%22%2C%20%22b%22%2C%20%22c%22))%0Adev.off()&quot;,&quot;editable&quot;:true,&quot;height&quot;:106,&quot;inputCode&quot;:&quot;&quot;,&quot;inputs&quot;:{},&quot;isVisible&quot;:true,&quot;label&quot;:&quot;Feel free to edit the R code&quot;,&quot;metadata&quot;:{&quot;name&quot;:&quot;r-barplot&quot;},&quot;mode&quot;:&quot;editor&quot;,&quot;outputs&quot;:[]}\">is loading &#8230;<\/div>\n\n\n\n<div class=\"wp-block-inseri-core-image\" data-attributes=\"{&quot;altText&quot;:&quot;&quot;,&quot;blockId&quot;:&quot;2HXz38wkfVbu9LXkynrut&quot;,&quot;caption&quot;:&quot;&quot;,&quot;fit&quot;:&quot;contain&quot;,&quot;height&quot;:null,&quot;inputKey&quot;:&quot;GYRJtLeZ3gBH9u1ud4H1a\/barplot.png&quot;,&quot;metadata&quot;:{&quot;name&quot;:&quot;image-barplot&quot;}}\">is loading &#8230;<\/div>\n\n\n\n<p>Next we will use <code>ggplot2<\/code> to achieve the same goal. There is no need to use <kbd><code>install.packages<\/code><\/kbd> because the missing packages are downloaded by default (see the first line of code below and the docs)<\/p>\n\n\n\n<div class=\"wp-block-inseri-core-r-code\" data-attributes=\"{&quot;autoTrigger&quot;:false,&quot;blockId&quot;:&quot;IkghIiUy6wdrAAldxEzBg&quot;,&quot;content&quot;:&quot;library(ggplot2)%0A%0A%23%20Create%20data%0Adata%20%3C-%20data.frame(%0A%20%20name%3Dc(%22a%22%2C%20%22b%22%2C%20%22c%22)%20%2C%20%20%0A%20%20value%3Dc(1%2C%203%2C%202)%0A%20%20)%0A%0A%23%20Barplot%0Aggplot(data%2C%20aes(x%3Dname%2C%20y%3Dvalue))%20%2B%20%0A%20%20geom_bar(stat%20%3D%20%22identity%22)%0A%20%20%0Aggsave(%22ggplot2.png%22)&quot;,&quot;editable&quot;:true,&quot;height&quot;:321,&quot;inputCode&quot;:&quot;&quot;,&quot;inputs&quot;:{},&quot;isVisible&quot;:true,&quot;label&quot;:&quot;Feel free to edit the R code&quot;,&quot;metadata&quot;:{&quot;name&quot;:&quot;r-ggplot&quot;},&quot;mode&quot;:&quot;editor&quot;,&quot;outputs&quot;:[]}\">is loading &#8230;<\/div>\n\n\n\n<div class=\"wp-block-inseri-core-image\" data-attributes=\"{&quot;altText&quot;:&quot;&quot;,&quot;blockId&quot;:&quot;PqC8wco0666fv38XHXktg&quot;,&quot;caption&quot;:&quot;&quot;,&quot;fit&quot;:&quot;contain&quot;,&quot;height&quot;:null,&quot;inputKey&quot;:&quot;IkghIiUy6wdrAAldxEzBg\/ggplot2.png&quot;,&quot;metadata&quot;:{&quot;name&quot;:&quot;image-ggplot&quot;}}\">is loading &#8230;<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:60%\"><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-inseri-core-export alignright\" data-attributes=\"{&quot;align&quot;:&quot;right&quot;,&quot;blockId&quot;:&quot;vaxQQCzmrnvQCf4meQgfk&quot;,&quot;metadata&quot;:{&quot;name&quot;:&quot;export&quot;},&quot;postId&quot;:1715}\">is loading &#8230;<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-inseri-core-share alignright\" data-attributes=\"{&quot;align&quot;:&quot;right&quot;,&quot;blockId&quot;:&quot;KTETwIissQyi0jL3ZpuwV&quot;,&quot;copiedText&quot;:&quot;Copied&quot;,&quot;metadata&quot;:{&quot;name&quot;:&quot;share&quot;},&quot;showIcon&quot;:true,&quot;text&quot;:&quot;Share&quot;}\">is loading &#8230;<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this example we will show you how to integrate our R code for generating plots into the R Code block and use the Image Box block to display it. For this use case please pay attention that we save the image as a PNG file. The PNG file is the output of our R [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[40,43,49,50],"class_list":["post-1715","post","type-post","status-publish","format-standard","hentry","category-basic-example","tag-export","tag-image-box","tag-r-code","tag-share"],"_links":{"self":[{"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/posts\/1715","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/comments?post=1715"}],"version-history":[{"count":1,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/posts\/1715\/revisions"}],"predecessor-version":[{"id":2134,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/posts\/1715\/revisions\/2134"}],"wp:attachment":[{"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/media?parent=1715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/categories?post=1715"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/tags?post=1715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}